D X
D X

Reputation: 41

Error in running the program in linux using g++

While compiling this using g++ on linux gives an error In function main': main.cpp:(.text+0x42): undefined reference toGradeBook::GradeBook(std::string)' collect2: error: ld returned 1 exit status

I am using this command to compile:

g++ -o gradebook.cpp gradebook.h main.cpp

// gradebook.cpp
#include <iostream>
using namespace std;
#include "gradebook.h"

GradeBook::GradeBook( string name )
{
 setCourseName( name );
 maximumGrade = 0;
}

void GradeBook::setCourseName( string name )
{
 if ( name.length() <= 25 )
  courseName = name;
 else 
  {
   courseName = name.substr( 0, 25 );
   cout << "Name \"" << name << "\" exceeds maximum length (25).\n"
    << "Limiting courseName to first 25 characters.\n" << endl;
  } 
}

string GradeBook::getCourseName()`
{
 return courseName;`
}

void GradeBook::displayMessage()
{
 cout << "Welcome to the grade book for\n" << getCourseName() << "!\n" << endl;
}

int maximum( int, int, int );
int maximumGrade;` 

void GradeBook::inputGrades()
{
 int grade1;
 int grade2;
 int grade3;
 cout << "Enter three integer grades: ";
 cin >> grade1 >> grade2 >> grade3;
}

int GradeBook::maximum( int x, int y, int z )`
{
 int maximumValue = x;
 if ( y > maximumValue )
  maximumValue = y;
 if ( z > maximumValue )
  maximumValue = z;
 return maximumValue;
}

void GradeBook::displayGradeReport()
{
 cout << "Maximum of grades entered: " <<  endl;
}

// gradebook.h

#include <string>

using namespace std;

class GradeBook
{
 public:
  GradeBook (string);
  void setCourseName (string);
  string getCourseName ();
  void displayMessage();
  void inputGrades ();
  void displayGradeReport (); 
  int maximum (int, int, int);
 private:
  string courseName;
  int maximumGrade;
};

// main.cpp

#include "gradebook.h"

int main (int argc, char*argv[])
{
 GradeBook  myGradeBook ("Introduction to C++");
 return 0;
}

Upvotes: 0

Views: 107

Answers (1)

user2033018
user2033018

Reputation:

GCC's -o option lets you specify the executable's/library's name, as in

g++ -o foo.exe foo.cpp

You forgot to add the name after the flag, so I assume it's taking gradebook.cpp as the output's name.

In your case, it should be

g++ -o my_prog gradebook.cpp main.cpp

Upvotes: 3

Related Questions