(对象)不命名类型.怎么回事
(object) does not name a type. Whats going on?
我认为代码最能说明问题
class blok
{ public:
sf::RectangleShape TenBlok;
int x,y;
blok(int posX ,int posY)
{
x = posX;
y = posY;
}
void place(int x,int y)
{
TenBlok.setPosition((float)x,(float)y)
}
};
[...]
class Trawa : public blok
{
int id = 0;
sf::Texture tekstura;
tekstura.loadFromFile("trawa.png"); //<---- here it says "tekstura does not name a type"
TenBlok.setTexture(tekstura); //<---- here it says "TenBlok does not name a type"
};
错误说对象没有命名类型,但奇怪的是,CodeBlocks将麻烦的tekstura和TenBlok视为有效的对象,因为这些对象包含id提示函数
不能使用语句
tekstura.loadFromFile("trawa.png"); //<---- here it says "tekstura does not name a type"
TenBlok.setTexture(tekstura); //<---- here it says "TenBlok does not name a type"
在类定义中。它们不是声明。您可以在成员函数的定义中包含此类语句,但不能在类本身中包含此类语句。
一个更简单的类,它将失败并出现类似的错误:
struct Foo
{
int i;
i = 10;
};
若要初始化i
(或执行类似的语句(,请使用构造函数。
struct Foo
{
int i;
Foo() { i = 10; } // For demonstration. It will be better to initialize
// i using Foo() : i(10) {}
};
对于您的课程,您可能需要:
class Trawa : public blok
{
int id = 0;
sf::Texture tekstura;
Trawa() : blok(0, 0) // Assume position to be (0, 0)
{
tekstura.loadFromFile("trawa.png");
TenBlok.setTexture(tekstura);
}
};
相关文章:
- 使用递归的数组的最小值.这是怎么回事
- 我不小心调用了一个没有自己类对象的成员函数.但这是怎么回事呢
- 我使用 OpenMP 的线程越多,执行时间就越长,这是怎么回事?
- 绕道重铸瓷砖层是怎么回事
- 架 (U+67B6) 与en_US不符.UTF-8.这是怎么回事?
- R 中的算术在数字上比整数更快。这是怎么回事?
- 我的代码运行良好,但现在当尝试将其制作成模板时,我遇到了许多看似无关的错误。这是怎么回事?
- 在变量名后声明带有 () 的非内部类型与不使用变量名的行为不同。即 std::map<int,char>x(); - 这是怎么回事?
- 弄清楚这段代码是怎么回事(递归下降案例研究)
- 分配器密集和稀疏是怎么回事?
- 传递引用C++,指针的向量.这是怎么回事?
- __vector_base_common是怎么回事?
- (对象)不命名类型.怎么回事
- 变量已在 .obj 中定义;这是怎么回事?
- Sublime上的EasyClangComplete是怎么回事?
- 重复输出的类型是怎么回事,它如何区分迭代次数和 Ascii 代码?
- 标准::地图使用.这是怎么回事?核心转储?我做得不正确?
- llvm libcxx的源代码是怎么回事?
- 这一行是怎么回事: istringstream is( line );
- "enable_if"使编译器很难生成可读的错误消息,这是怎么回事