对不起,我是一个初学者,我正在尝试运行以下视频中的代码:https://www.youtube.com/watch?v=gq2Igdc-OSI&ab_channel=thenewboston
下面是我的代码:
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();
};
#endifMother.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();
};
#endifDaughter.cpp
#include <iostream>
#include "Mother.h"
#include "Daughter.h"
using namespace std;
Daughter::Daughter()
{
}我的错误是:“未定义的引用`母亲::sayName()‘收集器2.exe:错误: ld返回1退出状态”我做错了什么?和视频一模一样,我是不是漏掉了什么?它们都在同一个文件夹中。我使用的是VS Code。
这是我的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:\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发布于 2021-11-03 22:27:01
通常,使用VSCommunity构建cpp可能更容易,因为这也是免费的(尽管可能比您需要的更重)。尝试从命令行构建程序,而不是通过VSCode来查看是否发生了链接。
https://stackoverflow.com/questions/69831934
复制相似问题