1

sorry I am a beginner and I am trying to run this code from this video here: https://www.youtube.com/watch?v=gq2Igdc-OSI&ab_channel=thenewboston

Here is my code:

main.cpp

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

int main() {
    Mother mom;
    mom.sayName();
}

Mother.h

#ifndef MOTHER_H
#define MOTHER_H

class Mother 
{
    public:
        Mother();
        void sayName();
};

#endif

Mother.cpp

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

Mother::Mother()
{
}

void Mother::sayName() {
    cout << "I am a Roberts!" << endl;
}

Daughter.h

#ifndef DAUGHTER_H
#define DAUGHTER_H

class Daughter 
{
    public:
        Daughter();
};

#endif

Daughter.cpp

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

Daughter::Daughter()
{

}

My error is: "undefined reference to `Mother::sayName()' collect2.exe: error: ld returned 1 exit status" What am I doing wrong? It's the exact same as the video, am I missing something? They are all in the same folder. I'm using VS Code.

here is my tasks.json file, what should i change?

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\Users\\Taro\\Desktop\\MinGW\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${workspaceFolder}\\*.cpp",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "compiler: C:\\Users\\Taro\\Desktop\\MinGW\\bin\\g++.exe"
        }
    ]
}

here is the error message in the terminal:

PS C:\Users\Taro\Desktop\CIS 250> cd "c:\Users\Taro\Desktop\CIS 250\" ; if ($?) { g++ main.cpp -o main } ; if ($?) { .\main }
c:/users/taro/desktop/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Taro\AppData\Local\Temp\ccBwABJt.o:main.cpp:(.text+0x15): undefined reference to `Mother::Mother()'
c:/users/taro/desktop/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Taro\AppData\Local\Temp\ccBwABJt.o:main.cpp:(.text+0x21): undefined reference to `Mother::sayName()'
collect2.exe: error: ld returned 1 exit status
drescherjm
  • 10,365
  • 5
  • 44
  • 64
taro
  • 11
  • 2
  • 2
    It appears that `Mother.cpp` is still not being compiled. Please show the exact command that was run in the terminal to compile your code. – drescherjm Nov 03 '21 at 21:28
  • thank you. I updated the exact message in the terminal – taro Nov 03 '21 at 21:33
  • 1
    You're not actually utilizing your `tasks.json`. Something else runs that command, probably the "code runner" extension. – HolyBlackCat Nov 03 '21 at 21:34
  • oh i see, how can i get this code to run? – taro Nov 03 '21 at 21:41
  • Are you using the code-runner extension? If not did you put your tasks.json file in the correct folder? – drescherjm Nov 03 '21 at 21:42
  • yes, i have the code runner extension installed. my tasks.json file is in a .vscode folder inside the correct folder – taro Nov 03 '21 at 21:44
  • You can remove code-runner or possibly try this advice: [https://stackoverflow.com/a/68729810/487892](https://stackoverflow.com/a/68729810/487892) – drescherjm Nov 03 '21 at 21:45
  • thank you very much for the suggestions! it still gives me the same error though :(( – taro Nov 03 '21 at 21:51
  • Do you have tasks.json in the proper folder? In the same folder as your cpp files there should be a .vscode folder. And in that folder you should have your `tasks.json` file. You should also have a `launch.json` file in that folder. – drescherjm Nov 03 '21 at 22:11
  • yup its in the correct folder as my cpp files and there is a launch.json, settings.json, and tasks.json – taro Nov 03 '21 at 22:13
  • 1
    I don't know why your tasks.json is not being used. I edited your question title and tags to better indicate the problem with VSCode – drescherjm Nov 03 '21 at 22:21

1 Answers1

0

Generally, it might be easier to build cpp with VSCommunity since that's also free (albeit may be heavier weight than you need). Try building the program from the command line rather than through VSCode to see if linking is happening at all.