0

I have a file called MyVector.cpp:

#include "MyVector.h"


MyVector::MyVector(double x ,double y ,double z){
    this->v[0] = x;
    this->v[1] = y;
    this->v[2] = z;
    this->v[3] = 1.0;

}

and header with the name MyVector.h:

#ifndef MYVECTOR_H
#define MYVECTOR_H

#include <iostream>


class MyVector{
    public:
        double v[4];

        MyVector(double x ,double y ,double z);

};

#endif

In the main file I try to use the class MyVector:

#include <iostream>
#include "MyVector.h"


int main(){
    MyVector v(1.0 ,2.0 ,3.0);

    std::cout << v.v[0] << "\n";
    std::cout << v.v[1] << "\n";
    std::cout << v.v[2] << "\n";
    std::cout << v.v[3] << "\n";

    return 0;
}

But all I get is an error saying the refrence to the constructor (MyVector) is undefined. undefined reference to MyVector::MyVector(double, double, double)`

I tried to change the names of the class but it didnt work

  • 3
    How do you compile and link your code? – MSpiller Jul 25 '23 at 12:11
  • @MSpiller I use vscode with extentions and my compiler is mingw – MRubiksCube Jul 25 '23 at 12:13
  • 1
    @MRubiksCube Have you done anything to configure VS Code to compile and link `MyVector.cpp` with `main.cpp`? I think VS Code shows the compiler command used in the build readout. Make sure that it includes both sources files, and not just `main.cpp`. – Brian61354270 Jul 25 '23 at 12:16
  • 2
    It's a common misunderstanding the header files like `MyVector.h` though some C++ magic allow other files to find the corresponding source file `MyVector.cpp`. This is not true. All sources files that are part of the program must be passed to the compiler and linker by you (or by your IDE). In your case this is not happening because you have not configured VSCode correctly. – john Jul 25 '23 at 12:27
  • The instructions you should have followed are [here](https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson). It's an easy detail to miss, we get your question quite frequently. – john Jul 25 '23 at 12:33
  • Remember that VSCode by default builds only the active file into your executable. The official documentation tells you that and how to change your `tasks.json` to have it build all files in your folder: [https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson](https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson) – drescherjm Jul 25 '23 at 12:34
  • 3
    VSCode is not a beginner-friendly IDE. I recommend uninstalling it and installing one that just works out of the box, without you having to fiddle with extensions and json files. Or just go with a simple text editor and a command line, you will learn more and faster this way. – n. m. could be an AI Jul 25 '23 at 12:37
  • [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) – Jesper Juhl Jul 25 '23 at 13:07

1 Answers1

0

Have you tried to compile by yourself using g++ ?

Something like this should work :

g++ -o vector main.cpp MyVector.cpp
Marc Agnetti
  • 76
  • 1
  • 11
  • It's good advice but it's not really an answer to the question asked. – john Jul 25 '23 at 12:43
  • @john I interpreted his problem as he couldn't compile his code because of its different classes, so I advised him to compile it directly from the compiler that's why ! At least the question needs to be clarified to highlight the real "problem". – Marc Agnetti Jul 25 '23 at 12:47
  • 1
    Is `MyVector.h` in there intentionally? You don't need to separately compile header files. – Brian61354270 Jul 25 '23 at 12:50
  • No it's not, I'm editing it, it was more likely to clarify, I prefer to add them personally – Marc Agnetti Jul 25 '23 at 12:52