c++ - C++ 未定义的错误引用(Visual Studio 代码)
问题描述
这是我遇到过的最奇怪的问题,一切都是正确的,并且已经过仔细审查,但程序仍然无法运行。我也在在线 IDE 上尝试了这段代码(zybooks;我大学的课程),它在那里运行得非常好。
我究竟做错了什么?
主文件
#include <iostream>
#include "Character.h"
using namespace std;
int main(){
Character character("ekko", "black", 100, 100);
cout << character.getName();
return 0;
}
字符.cpp
#include "Character.h"
#include <iostream>
Character::Character(){
name = "Summoner";
race = "NONRACIAL";
level = 0;
health = 0;
}
Character::Character(string name, string race, int level, int heatlh ){
this->name = name;
this->race = race;
this->level = level;
this->health = health;
}
void Character::setName(string name){ this->name = name; }
void Character::setRace(string race){ this->race = race; }
void Character::setLevel(int level){ this->level = level; }
void Character::setHealth(int health){ this->health = health; }
string Character::getName(){ return name; }
string Character:: getRace(){ return race; }
int Character:: getLevel(){ return level; }
int Character:: getHealth(){ return health; }
字符.h
#include <iostream>
using namespace std;
#ifndef CHARACTER_H
#define CHARACTER_H
class Character{
private:
string name;
string race;
int level, health;
public:
Character();
Character(string name, string race, int level, int heatlh );
string getName();
string getRace();
int getLevel();
int getHealth();
void setName(string name);
void setRace(string race);
void setLevel(int level);
void setHealth(int health);
};
#endif
错误:
[Running] cd "c:\Users\diono\OneDrive\Desktop\COSC_1430_RPG_GAME\Character\" && g++ tempCodeRunnerFile.cpp -o tempCodeRunnerFile && "c:\Users\diono\OneDrive\Desktop\COSC_1430_RPG_GAME\Character\"tempCodeRunnerFile
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o): in function `main':
C:\temp\gcc\build-mingw-w64\mingw-w64-crt/../../src/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
[Done] exited with code=1 in 0.883 seconds
解决方案
Visual Studio Code 只是编辑器编码环境,因此您需要为其添加更多编译工具,例如 Cmake 来执行您的代码。 https://code.visualstudio.com/docs/cpp/cmake-linux#:~:text=Install%20the%20C%2FC%2B%2B%20extension,a%20debugger%2C%20and%20build%20tools。
推荐阅读
- python - Python:模拟引发异常的模块
- java - 将 JConsole 输出记录到 CSV 文件中。时间价值背后的逻辑是什么?
- php - 在正文中创建包含表格的电子邮件时出现问题
- java - 在运行时动态创建实体 - 使用 Gorm orm 的 Spring boot、hibernate、Java 或 Grails?
- sql - ORA-32040 在过程中,如何重写查询
- java - 如何以快速采样频率(例如,100Hz - 200Hz)有效地处理传入的 MQTT 消息
- python - 在python中搜索列表的最快方法
- java - java - 如何从Java中的CSV文件数据计算BMI(身体质量指数)?
- c - 将二维数组保存在指针数组中
- python - 调用另一个函数时函数出错