编译器给出错误:声明后立即"Does not name a type"
Compiler gives error: "Does not name a type" immediately after declared
我的项目中有一块代码,用于用ncurses构建c++roguelike。我正在努力让玩家能够持有两件武器。我已经创建了一个weaponItem类和两个对象,但编译器仍然抛出"不命名类型"错误。
代码:
weaponItem weapon1;
weaponItem weapon2;
weapon1.setType(DMW_DAGGER);
weapon2.setType(DMW_SBOW);
weapon1.setPrefix(DMWP_AVERAGE);
weapon2.setPrefix(DMWP_RUSTY);
编译器错误:
In file included from main.cpp:2:0:
hero.h:17:2: error: ‘weapon1’ does not name a type
weapon1.setType(DMW_DAGGER);
^
hero.h:18:2: error: ‘weapon2’ does not name a type
weapon2.setType(DMW_SBOW);
^
hero.h:20:2: error: ‘weapon1’ does not name a type
weapon1.setPrefix(DMWP_AVERAGE);
^
hero.h:21:2: error: ‘weapon2’ does not name a type
weapon2.setPrefix(DMWP_RUSTY);
^
我的类或对象声明有问题吗?
我认为您误解了错误消息和一些注释。
假设你有一个类/结构。
struct Foo
{
Foo() : a(0) {}
void set(int in) { a = 10; }
int a;
};
可以在函数定义之外定义Foo
类型的对象。
// OK
Foo foo1;
但是,不能在函数定义之外单独调用类的成员函数。
// Not OK in namespace scope or global scope.
foo1.set(20);
您可以在文件中的函数定义内进行函数调用。
// OK.
void testFoo()
{
foo1.set(20);
}
如果使用成员函数的返回值初始化另一个变量,则可以在函数定义之外调用该成员函数。
struct Foo
{
Foo() : a(0) {}
void set(int in) { a = 10; }
int get() { return a; }
int a;
};
// OK
Foo foo1;
int x = foo1.get();
相关文章:
- 将系数存储在头文件的数组中("does not name a type"错误)
- 当初始值设定项是基类名时'initializer does not name a non-static data member or base class'错误
- 由于"error C4430: missing type specifier - int assumed. Note: C++ does not support default-int",我现在无法编
- 如何解决"'mutex' in namespace 'std' does not name a type"?
- 在 C++ 中使用命名空间时出现"does not name a type"错误
- 已声明时"Does not name a type"错误
- 嵌套类"does not name a type"
- 这在C++ "It does not own the underlying data, and so is cheap to copy or assign"中意味着什么
- Eigen loadMarket does not compile
- 为什么当我的代码超出函数范围时,"does not name a type"出现编译器错误?
- 创建C++ Redis 模块 - "does not export RedisModule_OnLoad() symbol"
- 无法分隔标头类。我得到"does not name a type"
- 模板元编程:"does not have integral or enumeration type"
- 如何修复类函数"prototype does not match"和"cadidate is"错误
- #define 的"Declaration does not declare anything"错误
- 修复第三方代码:"error: ‘enable_if’ in namespace ‘std’ does not name a template type"
- 在 c++ 中,如果包含标头,为什么我会收到'does not a name of type'错误?
- 什么原因导致我的"‘FatalError’ in namespace ‘Foam’ does not name a type"错误?
- C++ 迭代器错误"does not refer to a value"
- 编译错误"String does not name a type"