代码在CodeBlocks中工作,但在linux中编译时会出错

时间:2013-12-05 08:21:31

标签: c++ linux

的main.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:错误:ISO C ++禁止成员'numLeg'的初始化   cootie.h:11:22:错误:使'numLeg'静止   cootie.h:11:22:错误:ISO C ++禁止非const静态成员'numLeg'的类内初始化

我尝试使用std = c ++ 11标志编译,但我得到了cc1plus:错误:无法识别的命令行选项“-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 to main'中 collect2:ld返回1退出状态

我对linux很新,所以我不确定是什么导致了这个问题。

2 个答案:

答案 0 :(得分:2)

您无法在&lt;类的定义中初始化实例变量(例如numLeg)。 C ++ 11X。你必须在构造函数中初始化它们,例如像这样:

cootie::cootie()
{
  numLeg = 0;
  numHead = 0;
  numEye = 0;
  numWing = 0;
  numBody = 0;
  numAntenna = 0;
  roll = 0;
}

只需在每个实例变量之前删除头文件中的= 0

答案 1 :(得分:0)

这一行是你的问题:

int numLeg = 0, numHead = 0, numEye = 0, numWing = 0, numBody = 0, numAntenna = 0, roll = 0;

您需要删除= 0,最后在构造函数中初始化它们。 I.o.w:

class cootie
{
public:
    int numLeg, numHead, numEye, numWing, numBody, numAntenna, roll;

    cootie::cootie()
    : numLeg(0)
    , numHead(0)
    , ...
    {
    }
};