[英]C++ class instance identifier undeclared
So, I'm trying to compile my code, but the compiler keeps complaining that "'mysnake' undeclared (first use this function)", but I declared it. 因此,我试图编译我的代码,但是编译器一直抱怨“未声明'mysnake'(首先使用此函数)”,但我声明了它。 This is my Main.cpp, wehre it is declared.
这是我的Main.cpp,在这里已声明。
#include "Class.h"
#include "Snake.h"
int main(int argc, char* args[]){
Prog run;
if((run.Init())==false){
return(1);
}
Snake mysnake;
if(run.LoadFiles()==false){
return(1);
}
run.MainLoop();
if(run.Draw()==false){
return(1);
}
run.CleanUp();
return(0);
}
And this is the file that makes the compiler complain (AFAIK it's the first file with any reference to 'mysnake' that gets compiled) 这是使编译器抱怨的文件(AFAIK是第一个引用了被编译的“ mysnake”的文件)
#include "Class.h"
#include<sstream>
#include "Snake.h"
bool Prog::Draw(){
std::stringstream message;
SDL_Rect position;
SDL_BlitSurface(image, NULL, screen, NULL);
int s=mysnake.EndSnake();
message<<"Your snake was "<<s<<" blocks long.";
msg=TTF_RenderText_Solid(font, message.str().c_str(), font_color);
if(msg==NULL){
return(false);
}
position.x=(WWIDTH-msg->w)/2;
position.y=(WHEIGHT-msg->h)/2;
SDL_BlitSurface(msg, NULL, screen, &position);
SDL_Flip(screen);
return(true);
}
I have thought about it for over an hour and I still can't understand why it does this. 我已经考虑了一个多小时,但我仍然不明白为什么这样做。 By the way I'm using Bloodshed Dev C++ I'd be very grateful for help.
顺便说一下,我正在使用Bloodshed Dev C ++,非常感谢您的帮助。
Inside your Draw
function there is no variable declared called mysnake
. 在
Draw
函数中,没有声明为mysnake
变量。 That function can't see the mysnake
that's declared in main
because it is local to main
. 该函数不能看到
mysnake
这是一个在声明main
因为它是本地的main
。 You need to pass your mysnake
object to the Draw
function so that it knows which snake you're actually talking about. 您需要将
mysnake
对象传递给Draw
函数,以便它知道您实际上在谈论哪条蛇。
To do that, give Draw
an argument of type const Snake&
, a "reference to const Snake
" (or take away the const
if EndSnake
is a non- const
member function): 为了做到这一点,给
Draw
类型的参数const Snake&
的,“参考const Snake
”(或带走const
如果EndSnake
是非const
成员函数):
bool Prog::Draw(const Snake& snake) {
// ...
}
And when you call Draw
in main
, do this: 当您在
main
调用Draw
时,请执行以下操作:
run.draw(mysnake);
Now your Draw
function has a variable called snake
which was passed in from main
. 现在,您的
Draw
函数具有一个名为snake
的变量,该变量是从main
传入的。 Because the argument is a reference, the Snake
object that it sees is exactly the same object as in main
. 因为该参数是引用,所以它看到的
Snake
对象与main
对象完全相同。 If the argument had been of type Snake
instead of const Snake&
, then you would get a copy of the mysnake
from main
. 如果参数的类型为
Snake
而不是const Snake&
,则可以从main
获得mysnake
的副本。
Some extra advice: 一些额外的建议:
We usually write conditions like (run.Init())==false
as just !run.init()
- it reads much better. 我们通常将条件写成
(run.Init())==false
只是!run.init()
-它的读取效果要好得多。 Returning is also usually written as return true;
Returning通常也写成
return true;
, rather than return(true);
,而不是
return(true);
, but that's up to you. ,但这取决于您。
The fact that mysnake
is declared in main
does not allow one to use it in Prog
. mysnake
在main
中声明的事实不允许在Prog
使用它。 You probably want to transmit a reference to mysnake to the Draw method. 您可能希望将对mysnake的引用传递给Draw方法。 Through the constructor or through the call to the method.
通过构造函数或通过对方法的调用。
Prog run(mysnake);
run.draw();
or 要么
run.draw(mysnake);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.