简体   繁体   English

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

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

main.cpp 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 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 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;
}

I keep running into an error that states that the variables and functions declared in cootie.h aren't able to be accessed in the main.cpp file. 我一直遇到一个错误,指出cootie.h中声明的变量和函数无法在main.cpp文件中访问。 Basically it says that they aren't located in its scope. 基本上它说它们不在其范围内。 Any help would be great, i can't figure this out. 任何帮助都会很棒,我无法弄清楚这一点。

Edit: No longer getting the error of the functions and variables not being in the main.cpp scope, but now its saying that the "cootie c;" 编辑:不再得到函数和变量的错误不在main.cpp范围内,但现在它说“cootie c;” has an incomplete type. 有一个不完整的类型。

Fixed it, I forgot to put in the arguments of my print function, everything works now, thanks for the help! 修复它,我忘了把我的打印功能的参数,现在一切正常,谢谢你的帮助!

You have calls similar to this one: 您有与此类似的电话:

setName(name);

of a non member function setName that is not defined. 未定义的非成员函数setName说明。 The function you probably meant to call is the member function of the class cootie . 您可能要调用的函数是类cootie的成员函数。 That should be called on an object of that class, like this: 应该在该类的对象上调用它,如下所示:

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

In your main.cpp try accessing them as: 在你的main.cpp中尝试访问它们:

cootie::<variable or function>

I also realized that those aren't static functions, you need to add a cootie object: 我也意识到那些不是静态函数,你需要添加一个cootie对象:

cootie myNewCootie;
myNewCootie.<functions and whatnot>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM