0

I'm basically trying to instantiate an object from a class in c++ and use one of the member functions. This feels like a pretty standard problem, but all of the solutions I find online are either simple bracket issues, or scope resolution stuff that seems really obvious, or massively complex examples that shroud what's actually going on in over-complexity. I really appreciate anyone that Might be able to help me understand what I'm doing wrong with these files.

The errors I get are

undefined reference to Test::Test()'

undefined reference to Test::msg()'

I have three files, a main, a Test.hpp, and Test.cpp.

main.cpp

#include "Test.hpp"
#include <iostream>
using namespace std;

int main(){

    Test var;
    var.msg();

    return 0;
}

Test.hpp

#ifndef TEST_HPP
#define TEST_HPP

class Test{
public:
    Test();
    void msg();
};
#endif

Test.cpp

#include "Test.hpp"
#include <iostream>
using namespace std;

Test::Test(){
    cout << "instantiated\n\n";
}
void Test::msg(){
    cout << "Hello\n\n";
}
Community
  • 1
  • 1

1 Answers1

0

Considering you use codeblocks as your IDE just go to: project settings -> project build options -> search directories -> add and locate where your .cpp and .h files are. Then it will ask you if you want to keep this as relative path. Say no.

If you using some other ide its almost the same proccess, just comment me and i will provide you the steps.

Btw there is no need to include iostream in main since you have already included it in test.

KostasRim
  • 2,053
  • 1
  • 16
  • 32
  • I'm actually using g++ as my compiler instead of codeblocks. Does the linking happen differently depending on the IDE/compiler one uses? It's starting to seem like that might be my main problem. – Paul Ronquillo Nov 03 '15 at 17:08
  • $ gcc -o output file1.o file2.o and you are done. let me know if it worked, where file1 and file 2 are your files(use your file names instead). and this $ gcc -c file1.c to get the object file. – KostasRim Nov 03 '15 at 17:25
  • This link below shows a similar method. Looks like this worked. To be clear for other people dabbling with the same setup. I had to run g++ -o programName main.cpp file1.cpp file2.cpp without header files in the command. and I was able to get this to compile using g++ http://stackoverflow.com/questions/3202136/using-g-to-compile-multiple-cpp-and-h-files – Paul Ronquillo Nov 03 '15 at 17:40
  • glad it worked for you :) – KostasRim Nov 03 '15 at 18:06