Main.cpp无法访问头文件和其他.cpp文件中的变量和函数

时间:2013-12-05 06:14:54

标签: c++

的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 = 0, numHead = 0, numEye = 0, numWing = 0, numBody = 0, numAntenna = 0, roll = 0;
        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()
{

}

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;
}

我一直遇到一个错误,指出cootie.h中声明的变量和函数无法在main.cpp文件中访问。基本上它说它们不在其范围内。任何帮助都会很棒,我无法弄清楚这一点。

编辑:不再得到函数和变量的错误不在main.cpp范围内,但现在它说的是“cootie c;”有一个不完整的类型。

修正了它,我忘记了我的打印功能的参数,现在一切正常,感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

你有类似这个的电话:

setName(name);

未定义的非成员函数setName。您可能要调用的函数是类cootie的成员函数。应该在该类的对象上调用它,如下所示:

cootie c;
c.setName(...);

答案 1 :(得分:0)

在你的main.cpp中尝试访问它们:

cootie::<variable or function>

我也意识到那些不是静态函数,你需要添加一个cootie对象:

cootie myNewCootie;
myNewCootie.<functions and whatnot>