0

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;
}
aestrivex
  • 5,170
  • 2
  • 27
  • 44
D X
  • 41
  • 1
  • 1
  • 3

1 Answers1

3

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
  • Can you tell me the exact command of how to compile it ? I am struggling to do it. Sorry if my question sounds very stupid. – D X Nov 07 '13 at 20:58
  • When i use the command you told me it says my_prog directory not found. I have also created a file with the name as my_prog. How should I proceed. – D X Nov 07 '13 at 21:14
  • Well, I've never used GCC under Linux, so I don't know how to name an "executable" there (if that concept even exists). Simply don't specify any name: g++ gradebook.cpp main.cpp –  Nov 07 '13 at 21:18
  • @DX delete the file my_prog you created. The compiler will be making the output for you. (You could leave out the `-o` option alternatively, usually gcc uses the .out extension such as `a.out` by default) –  Nov 07 '13 at 21:20
  • Its g++ not gcc.. gcc is for compiling c programs – D X Nov 07 '13 at 21:21
  • @DX Yes, well... if I were at a command line I'd be typing the right one. They are both GNU and both compilers and very related. see: http://stackoverflow.com/questions/172587/what-is-the-difference-between-g-and-gcc –  Nov 07 '13 at 21:22
  • Whenever I say GCC, I'm referring to the GNU Compiler Collection, which includes gcc (a C compiler) and g++ (a C++ compiler), among others. –  Nov 07 '13 at 21:22
  • Thanks anyway but I can't run it.. I will try to figure out. – D X Nov 07 '13 at 21:25
  • @DX What's the error message? Is it still something about a directory? (Maybe try a quick hello.cpp see that compiles) –  Nov 07 '13 at 21:52
  • I am following a tutorial it says that I should use the command like: g++ -o main main.cpp gradebook.cpp ... This will create an executable file with the name of main instead of a.out.. Now I don't know how to see the output of the code.. It should print now ... Introduction to computer programming C++ – D X Nov 07 '13 at 21:55