C++静态成员的链接器错误
C++ linker errors with static members
class fairy
{
protected:
bool x;
int points;
public:
fairy();
void intro(string, int);
void call_decision(int, int);
};
fairy::fairy()
{
x = true;
points = 0;
}
void fairy::intro(string NAME, int case_type)
{
int decision = 0;
cout << "Hello, my name is" << NAME << "." << endl << "What would you like to do" << endl;
while(x)
{
cout << endl;
cout << "(1) Ask for a wish." << endl;
cout << "(2) Offer to help the fairy." << endl;
cout << "(3) Say goodbye to the fairy." << endl;
cin >> decision;
call_decision(decision, case_type);
}
}
class good : fairy
{
protected:
int wish_num;
bool HELP;
public:a
good();
static string name;
void wish();
void help();
};
good::good()
{
name = "Sandy";
wish_num = 0;
HELP = true;
}
void good::wish()
{
if (wish_num != 5)
{
cout << "Your wish has been granted" << endl;
points += 10;
wish_num++;
}
else
{
cout << "Sorry, I can not grant you another wish" << endl;
}
}
void good::help()
{
if (HELP)
{
cout << "THANK YOU" << endl;
points += 50;
HELP = false;
}
else
{
cout << "Thank you but I don't need anymore help." << endl;
}
}
class evil : fairy
{
protected:
public:
evil();
static string name;
void wish();
void help();
};
evil::evil()
{
name = "Seth";
}
void evil::wish()
{
cout << "NO!!!! MUHHHHHAAAA!!!" << endl;
points -= 10;
}
void evil::help()
{
cout << "Thanks... NOT, I don't even want your help!!" << endl;
points -= 50;
}
class equivocal : fairy
{
protected:
int wish_num;
public:
equivocal();
static string name;
void wish();
void help();
};
equivocal::equivocal()
{
name = "Joe";
wish_num = 0;
}
void equivocal::wish()
{
int random = (rand() % 2);
if (random && wish_num != 3)
{
cout << "Your wish has been granted" << endl;
points += 10;
wish_num++;
}
else
{
cout << "Sorry, I can not grant you another wish" << endl;
points -= 10;
}
}
void equivocal::help()
{
cout << "I'll think about it" << endl;
}
void fairy::call_decision(int decision, int case_type)
{
good one;
evil two;
equivocal three;
{
switch(decision)
{
case'1':
{
switch (case_type)
{
case '0':
one.wish();
case '1':
two.wish();
case '2':
three.wish();
}
}
case '2':
{
switch (case_type)
{
case '0':
one.help();
case '1':
two.help();
case '2':
three.help();
}
}
case '3':
{
x = false;
}
}
}
}
int main()
{
fairy game;
good one;
evil two;
equivocal three;
string NAME;
int case_type = 0;
int num = rand();
for (int i = 0; i < 3; i++)
{
switch (num % 3)
{
case '0':
NAME = one.name;
case_type = 0;
case '1':
NAME = two.name;
case_type = 1;
case '2':
NAME = three.name;
case_type = 2;
default:
case_type = -1;
}
game.intro(NAME, case_type);
num++;
}
}
是否可以在所有派生类之后定义fairy::call_decision
?(这允许我在派生类的基类中进行引用。
如果是这样,为什么我在尝试编译时会收到 lnk 2001 错误?
如果没有,我怎样才能让它使用void fairy::intro
定义后定义的void fairy::call_decision
进行编译?
您遇到问题的原因是因为您std::string name
都被声明为static
。必须在命名空间范围内定义类定义之外static
成员,如下所示:string good::name = "Sandy";
大多数编译错误是因为您没有包含string
类型或cout
的头文件。 将以下内容放在文件的顶部:
#include <iostream>
#include <string>
using namespace std;
相关文章:
- Netbeans 10:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- CMake 错误:链接器命令失败,退出代码为 1 和 cpp.o 文件
- clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用) - 体系结构的未定义符号 x86_64:
- 如何修复 clang: 错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- Android NDK.Build命令失败.未定义的引用.clang++:错误:链接器命令失败,退出代码为1
- 使用cmake错误链接boost日志
- 错误:链接器命令失败,退出代码为 1(使用 -v 查看调用):在 Macbook 上
- C++ XCODE ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -
- clang:错误:链接器命令失败,C++代码中的退出代码为 1(使用 -v 查看调用)
- 静态库中的 g++ 错误链接函数
- 未定义的参考错误链接天然函数Android
- C++编译错误:ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -v
- QT Q_PROPERTY错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- OSX MOJAVE -LD:架构X86_64 clang找不到符号:错误:链接器命令失败,出口代码1
- C 错误链接器命令失败了出口代码1(使用-V查看调用)
- LD:架构x86_64 clang找不到符号:错误:链接器命令失败,出口代码1(使用-v to See
- 聚输出错误 - 链接列表
- Xcode链接器错误:链接器命令失败,退出代码为1(使用-v查看调用)
- 尝试构建C DLIB示例无法与数百个未定义的参考错误链接