0

I wrote this multiple file in c++ (just getting an integer as input and giving back the same number) and I placed all of them in the same directory. When I try to build the main.cpp with CodeBlocks I get the following error: In function 'main', undefined reference to 'grade::show(int)'. Does anyone know what could be the mistake in the code???

PS. It is exactly the same code that I find on my college book.

main.cpp

#include <iostream>
#include "grade.h"
using namespace std;

int main() {
int n1;
grade number;

cin >> n1;
cout << number.show(n1) << endl;
}

grade.h

#include <iostream>

class grade {
    public:
        int show(int);
};

grade.cpp

#include <iostream>
#include "grade.h"
using namespace std;

int grade::show(int n2) {
    return n2;
}
  • You have to compile **and link** `grade.cpp` to your final program. – πάντα ῥεῖ Sep 08 '15 at 15:51
  • It would be good to read up on `makefiles`, maybe in this SO post: http://stackoverflow.com/questions/2481269/how-to-make-simple-c-makefile – sunny Sep 08 '15 at 15:54
  • @sunny I'm afraid it's not a problem related to makefiles. @OP As you where asking for CodeBlocks, make sure `grade.cpp` is added as source to your current project. See [here](http://forum.openframeworks.cc/t/adding-source-and-header-files-to-project-in-codeblocks/18632) also please. – πάντα ῥεῖ Sep 08 '15 at 17:42
  • @ πάντα ῥεῖ This will be a problem OP has to deal with in future, and certainly best practice as soon as you've got multiple files is putting everything in a makefile. – sunny Sep 08 '15 at 17:44
  • @sunny _"as soon as you've got multiple files is putting everything in a makefile"_ Or use a decent IDE, that generates one for you. – πάντα ῥεῖ Sep 08 '15 at 17:47

0 Answers0