main.cpp 无法访问头文件和其他.cpp文件中的变量和函数
Main.cpp can't access variables and functions in header file and other .cpp file
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>
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 命名空间中具有.h和.cpp文件的类
- 为什么我的主文件.cpp不打印头文件中的任何内容
- 生成文件错误 - 找不到文件 - *.cpp
- 如何在文件.cpp gtkmm中声明小部件
- 没有实现文件(.cpp)的派生类
- 如何使用"CMakeLists.txt"中的add_library将整个文件(.cpp,.h等)包含在目录中
- 如何在Linux / Windows操作系统上使用文件*.cpp一步编译下面的代码
- 关于 Linux .so 文件无法链接到主文件.cpp文件
- 文件.cpp从Windows到Linux
- Ubuntu C++ 编译器错误: g++: 错误: 文件.cpp: 没有这样的文件或目录
- 如何将文件.cpp编译为本机编译
- 使用模板类时,似乎无法包含除 main 以外的任何 cpp 文件.cpp
- 无法编译C++文件.cpp。C++98模式
- 为什么C++头文件不需要包含实现文件 (.cpp)
- 如何访问位于独立文件(.cpp)中的非成员函数
- 如何在Visual Studio Code中通过键盘快捷键切换头文件/cpp文件
- 如何将数据从stringstream写入文件(CPP)
- 为什么filestream不把空白写入文件cpp ?