0

I included the header file in both the definitions file and int the main file. But I don't understand why I get these errors:

undefined reference to `Test::Test()'
undefined reference to `Test::Test(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, char)'
undefined reference to `operator<<(std::ostream&, Test const&)'
undefined reference to `operator<<(std::ostream&, Test const&)'

I split the code like below. What am I missing? Thanks for your answers!

test.h file, with declarations:

class Test
{
public:
    Test(int i1, string s1, char c1);
    //const Test &default_test();
    Test();
    int get_i() const { return i; }
    string get_s() const { return s; }
    char get_c() const { return c; }

private:
    int i;
    string s;
    char c;
};
ostream &operator<<(ostream &os, const Test &t);

test.cpp file, with definitions:

#include "test.h"   

Test::Test(int i1, string s1, char c1)
        : i{i1}, s{s1}, c{c1}
    {
    }
    const Test &default_test()
    {
        static Test t{1, "test1", 't'};
        return t;
    }
    Test::Test()
        : i{default_test().get_i()},
          s{default_test().get_s()},
          c{default_test().get_c()}
    {
    }
    ostream &operator<<(ostream &os, const Test &t)
    {
        return os << t.get_i() << " " << t.get_s() << " " << t.get_c() << "\n";
    }

and main.cpp file, with the actual program:

#include "test.h"

int main()

{
    Test test1;
    Test test2(2, "test2", 't');
    cout << "test1: " << test1;
    cout << "test2: " << test2;
}
Edric
  • 24,639
  • 13
  • 81
  • 91
Theodore
  • 179
  • 12
  • 3
    Are you compiling `test.cpp` as well? Please show your build command. – cigien Jun 26 '20 at 13:33
  • Looks like you split correctly but your problem is in your build process. is this yet another Visual Studio Code question? – drescherjm Jun 26 '20 at 13:35
  • when I'm trying to compile test.cpp I get: undefined reference to `WinMain' -- it doesn't have a main function. how to fix this? just add an empty main function? Thanks! – Theodore Jun 26 '20 at 13:36
  • 2
    ***I get: undefined reference to `WinMain'*** That means you have selected the wrong project type a GUI application instead of a console application. – drescherjm Jun 26 '20 at 13:37
  • 1
    You need to link the compiled source files together. How you do that depends on what tools you're using. – molbdnilo Jun 26 '20 at 13:37
  • I'm using Visual Studio Code (on Windows) – Theodore Jun 26 '20 at 13:39
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – ChrisMM Jun 26 '20 at 13:39
  • The instructions for adding more than one source file in Visual Studio Code are here: [https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson](https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson) – drescherjm Jun 26 '20 at 13:41
  • 1
    thanks guys! I'll read the links now – Theodore Jun 26 '20 at 13:43
  • I edited the task.json to build multiple C++ files by using "${workspaceFolder}\\*.cpp" instead of ${file}... but now I get: g++.exe: error: D:\PPPCpp\*.cpp: Invalid argument g++.exe: fatal error: no input files What else should I do? Thanks a lot! – Theodore Jun 27 '20 at 15:52

0 Answers0