0

im a french dev (sorry for my bad english). Im working on a C++ project. I cannot build my project if im including .h files ... its compiling only when im including .cpp files. Im using visual code, the extension from Microsoft for C/C++ and MinGW

Here is my command line to compile

g++ -g main.cpp -o main.exe

And here is my code :

method.cpp

#include <string>
#include "../Include/method.h"

using namespace std;

Method::Method(string methodName,string modifier,string returnType,string parameters)
{
    this->methodName= methodName;
    this->modifier = modifier;
    this->parameters = parameters;
    this->returnType = returnType;
}

method.h

#include <string>

using namespace std;

class Method
{

public:
    Method(string methodName,string modifier,string returnType,string parameters);

private:
    string methodName;
    string returnType;
    string parameters;
    string modifier; 
};

class.h

#include "../Include/method.h" // replace it to "../Members/method.cpp" and it will compile !!

using namespace std;

class Class
{

public:
    Class(vector<string> lines);
    bool Build();

private:
    vector<string> lines;
    bool BuildMethods();
    bool BuildFields();
    vector<Method*> methods;
};

and finally, class.cpp

#include "../Include/class.h"
#include <string>
#include <regex>


using namespace std;

const string METHOD_PATTERN = "(public|private) (\\w+) (\\w+)\\((.*?)\\)";

Class::Class(vector<string> lines)
{
    this->lines = lines;
}
bool Class::Build()
{
    return BuildMethods() && BuildFields();
}
bool Class::BuildMethods()
{
    for (int i = 0;i < lines.size();i++)
    {
        string line  = lines[i];

        regex r{METHOD_PATTERN, regex_constants::ECMAScript};

        smatch match;

        regex_search(line, match, r);

        if (match.size() > 0)
        {
            string modifier = match[1];
            string returnType = match[2];
            string methodName = match[3];
            string parameters = match[4];

            Method * method = new Method(methodName,modifier,returnType,parameters);



          //  this->methods.push_back(method);


        }
    }
    return true;
}
}

I get an error on this line (in class.cpp) :

Method * method = new Method(methodName,modifier,returnType,parameters);

here is my error

C:\Users\User\AppData\Local\Temp\ccq80SJg.o: In function `ZN5Class12BuildMethodsEv':
c:/users/User/desktop/nova/builder/members/class.cpp:38: undefined reference to `Method::Method(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2.exe: error: ld returned 1 exit status
The terminal process terminated with exit code: 1

its like the compiler do not find the .h , but its in project..and i dont understand why it is working with the .cpp files.

as i have said, if i replace this line in class.h

#include "../Include/method.h"  

with this one :

#include "../Members/method.cpp"

Its compiling and working. But i know i shouldnt have to include the .cpp file

Ty for reading

Skinz
  • 3
  • 1
  • [Using G++ to compile multiple .cpp and .h files](https://stackoverflow.com/a/3202161) – 001 Dec 11 '19 at 03:23

1 Answers1

0

You need to include the source files in the compilation statement ie,

g++ main.cpp class.cpp method.cpp -o main.exe
traintraveler
  • 351
  • 2
  • 9