VSCode只生成main.cpp文件,而不是文件夹中的所有文件



很抱歉,我是一个初学者,我正试图在这里运行此视频中的代码:https://www.youtube.com/watch?v=gq2Igdc-OSI;ab_channel=上的新支柱

这是我的代码:

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;
}

女儿.h

#ifndef DAUGHTER_H
#define DAUGHTER_H
class Daughter 
{
public:
Daughter();
};
#endif

子板.cpp

#include <iostream>
#include "Mother.h"
#include "Daughter.h"
using namespace std;
Daughter::Daughter()
{
}

我的错误是:;对"Mother::sayName(("collect2.exe的未定义引用:error:ld返回1个退出状态"我做错了什么?它和视频完全一样,我是不是遗漏了什么?它们都在同一个文件夹中。我正在使用VS代码。

这是我的tasks.json文件,我应该更改什么?

{
"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"
}
]
}

这是终端中的错误消息:

PS C:UsersTaroDesktopCIS 250> cd "c:UsersTaroDesktopCIS 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:UsersTaroAppDataLocalTempccBwABJt.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:UsersTaroAppDataLocalTempccBwABJt.o:main.cpp:(.text+0x21): undefined reference to `Mother::sayName()'
collect2.exe: error: ld returned 1 exit status

通常,使用VSCommunity构建cpp可能更容易,因为它也是免费的(尽管可能比您需要的更重(。尝试从命令行而不是通过VSCode构建程序,看看是否正在进行链接。

最新更新