与C++ "was not declared in this scope"作斗争
Struggling with C++ "was not declared in this scope"
有人能告诉我为什么我在运行这个时得到错误"name没有在作用域中声明"吗?
谢谢。
class lrn11_class{
public:
void setName(string x){
name = x;
}
string getName(){
return name;
}
private:
string lrn11_name;
};
int main()
{
lrn11_class lrn11_nameobject;
lrn11_nameobject.setname("Zee");
cout << lrn11_nameobject.getname() << endl;
return 0;
}
这应该有效-请参阅注释(BTW使用std::
-为什么"使用命名空间std"被认为是坏做法?)
#include <iostream>
#include <string>
class lrn11_class{
public:
void setName(const std::string& x){ // Potentially saves copying overhead
name = x;
}
std::string getName() const { // Look up const and its uses
return name;
}
private:
std::string name; // - Used: string lrn11_name; but functions use name!
};
int main()
{
lrn11_class lrn11_nameobject;
lrn11_nameobject.setName("Zee"); // Fixed typo
std::cout << lrn11_nameobject.getName() << std::endl; // Ditto
return 0;
}
- 您已经将lrn11_name声明为该类的可变成员。但在set和get函数中,您使用的是name
- 除了您需要调用您定义的函数之外。因此:lrn11_nameobject.setname("Zee");cout<lt;lrn11_nameobject.getname()<lt;endl
您必须使用以下代码:-
lrn11_nameobject.setName("Zee");
cout << lrn11_nameobject.getName() << endl;
确保
#include <iostream> using namespace std;
应包括在内。
相关文章:
- 如何修复"error: ‘_1’ was not declared in this scope"?
- 类中的数组变量C++导致"was not declared in this scope"实现文件的一个函数中错误,但在构造函数中不会导致错误
- 如何解决"cout was not declared in this scope"错误?
- 为什么程序显示我"was not declared in this scope"?
- 即使包含 <time.h 后仍"‘CLOCK_PER_SEC’ was not declared in this scope"错误>
- 我正在接受"Error: string was not declared in this scope"
- C++ 我一直在"was not declared in this scope error"
- 将 .ui 文件导入 Qt5 C++时出错"was not declared in this scope"
- 编译 CPP 应用程序时出错。 "error: 'posix_memalign' was not declared in this scope"
- main.cpp中的错误:函数库中的函数"not declared in this scope",但实际上是
- "Error: 'dungeon_t' was not declared in this scope"但包含在标题中
- "pointer was not declared in this scope"是什么意思
- "error: not declared in this scope?"是什么意思
- 宣布时的另一个'not declared in this scope'问题
- " 'X' not declared in this scope "错误
- 子类模板错误 (C++): "error: 'root' was not declared in this scope"
- Eclipse C++ Linux "to_string was not declared in this scope"
- 编译错误 C++ "was not declared in this scope" "expected type specifier"等
- "<varName> was not declared in this scope" C++中使用模板和继承
- 构建 tesseract3 训练模块时出现意外错误:"not declared in this scope"