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

Main.cpp can't access variables and functions in header file and other .cpp file

本文关键字:文件 cpp 变量 函数 其他 访问 main      更新时间:2023-10-16

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;"有一个不完整的类型。

修正了它,我忘了输入打印函数的参数,现在一切正常,谢谢你的帮助!

您有类似的调用:

setName(name);

未定义的非成员函数CCD_ 1。您可能想要调用的函数是类cootie的成员函数。应该在该类的对象上调用,比如:

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

在您的main.cpp中,尝试以以下方式访问它们:

cootie::<variable or function>

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

cootie myNewCootie;
myNewCootie.<functions and whatnot>