主要.cpp
#include <iostream>
#include <string>
#include <cstdlib>
#include "cootie.h"
using namespace std;
int main()
{
cout << "Time to create a Cootie!" << endl;
cootie c;
c.setName(name);
cout << "Add body parts." << endl;
cout << "1) Body" << endl << "2) Head" << endl << "3) Legs" << endl << "4) Wings" << endl << "5) Antennas" << endl << "6) Eyes" << endl;
cout << "Input 7 to print.";
while (roll != 7)
{
cin >> roll;
if (roll == 1)
{
c.setBody(numBody);
}
if (roll == 2)
{
c.setHead(numHead);
}
if (roll == 3)
{
c.setLeg(numLeg);
}
if (roll == 4)
{
c.setWing(numWing);
}
if (roll == 5)
{
c.setAntenna(numAntenna);
}
if (roll == 6)
{
c.setEye(numEye);
}
if (roll == 7)
{
c.print();
}
}
}
cootie.h
#ifndef COOTIE_H
#define COOTIE_H
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class cootie
{
public:
cootie();
int numLeg, numHead, numEye, numWing, numBody, numAntenna, roll;
string name = "Undefined";
int setName(string& name);
int setLeg(int& numLeg);
int setHead(int& numHead);
int setEye(int& numEye);
int setWing(int& numWing);
int setBody(int& numBody);
int setAntenna(int& numAntenna);
void print(string name, int numLeg, int numHead, int numEye, int numWing, int numBody, int numAntenna);
};
#endif // COOTIE_H
cootie.cpp
#include "cootie.h"
cootie::cootie()
{
numLeg;
numHead;
numEye;
numWing;
numBody;
numAntenna;
roll;
name = "Undefined";
}
int cootie::setName(string& name)
{
cout << "Name your Cootie!" << endl;
getline(cin, name);
}
int cootie::setBody(int& numBody)
{
numBody++;
}
int cootie::setHead(int& numHead)
{
numHead++;
}
int cootie::setWing(int& numWing)
{
numWing++;
}
int cootie::setLeg(int& numLeg)
{
numLeg++;
}
int cootie::setEye(int& numEye)
{
numEye++;
}
int cootie::setAntenna(int& numAntenna)
{
numAntenna++;
}
void cootie::print(string name, int numLeg, int numHead, int numEye, int numWing, int numBody, int numAntenna)
{
cout << "Cootie called " << name << endl;
cout << numLeg << " Leg(s)" << endl;
cout << numHead << " Head(s)" << endl;
cout << numEye << " Eye(s)" << endl;
cout << numWing << " Wings(s)" << endl;
cout << numBody << " Body(s)" << endl;
cout << numAntenna << " Antenna(s)" << endl;
}
我试图在 Linux 中编译这些文件,但我无法将 cootie.cpp 编译成源代码。我尝试使用“g++ -c cootie.cpp”,但最终出现错误。
cootie.h:11:22: error: ISO C++ forbids initialization of member 'numLeg' cootie.h:11:22: error: making 'numLeg' static cootie.h:11:22: error: ISO C++ forbids in-class initialization of non-const static member 'numLeg'
我尝试使用 std=c++11 标志进行编译,但我得到了 cc1plus: error: unrecognized command line option "-std=c++11"作为错误,所以我尝试了 std=c++0x 但是这给了我最初的错误。
修改了代码,但现在我在使用 std=c++0x 标志编译时收到此错误
/usr/lib64/gcc/x86_64-slackware-linux/4.5.2/../../../../lib64/crt1.o: 在函数 _start':
/glibc-tmp-6341ca4bac93b935314b1241c89966aa/glibc-2.13/csu/../sysdeps/x86_64/elf/start.S:109: undefined reference
main'
collect2: ld 返回 1 个退出状态
我是 linux 的新手,所以我不太确定是什么导致了这个问题。
最佳答案
您不能在
cootie::cootie()
{
numLeg = 0;
numHead = 0;
numEye = 0;
numWing = 0;
numBody = 0;
numAntenna = 0;
roll = 0;
}
只需删除头文件中每个实例变量之前的 = 0
。
关于c++ - 代码在 CodeBlocks 中工作,但在 linux 中编译时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20394837/