在构造函数 'cout' 中未在此范围内声明
in constructor 'cout' was not declared in this scope
我想练习一些基本的 C++ 知识。我专注于继承只是为了试验一下。一切顺利,直到我遇到一些奇怪的问题:'cout' 未在此范围内声明。我已经查看了一些主题,但在大多数主题上,提示就像追加库或写 'using namespace std' 但它没有解决我的问题。
#include <iostream>
class podst
{
public:
float a;
float b;
float dodaw();
podst(float c,float d) : a(c), b(d)
{
}
};
float podst::dodaw()
{
return (a+b);
}
class poch : public podst
{
poch() : podst(5,4)
{
cout << a << endl << b << dodaw() << endl;
}
};
using namespace std;
int main()
{
podst podst(1,2);
cout << podst.dodaw() << endl;
poch poch2;
return 0;
}
poch
从全局命名空间调用 cout
,而不是从 std
命名空间。请注意 using namespace std;
指令实际上在它下面,而不是上面。如果您不想每次都写整个命名空间前缀,最好将该指令放在每个函数的基础上,而不是每个文件的基础上:
poch() : podst(5,4)
{
using namespace std;
cout << a << endl << b << dodaw() << endl;
}
这种方法将有助于将大型代码库保持完整。
using namespace std 低于您第一次使用 cout 时的值。
尝试在 poch class.
中使用 std::cout 和 std::endl
using声明从你写的地方开始生效。由于对 cout
的调用高于该点,因此它不在 using
范围内,您必须在那里拼写 std::cout
。
You should avoid using namespace std
。一个更好的选择是只引入你需要的东西,例如using std::cout;
。但更好的选择是只使用限定名称。打字有点多,但可以防止歧义;在我所见过的每个代码库中,限定名称无处不在——至少对我来说——像 cout
这样的非限定名称总是有点令人惊讶,并且在阅读代码时会稍微绊倒我。
好吧,因为 poch 是 class 从父 class 继承的,你需要
在文件的开头声明标准命名空间,使其对所有 classes 和函数都是全局的,但是你在 main 之前声明了它,所以 poch class 无法使用它,而你可以已明确使用 std::cout 和 std::endl,程序编译不会有问题。
class poch : public podst
{
poch() : podst(5,4)
{
//you should have used std::cout
std::cout << a << endl << b << dodaw() << std::endl;
}
};
如果你想省略标准库命名空间调用者::std,你应该写成
using namespace std;
之前(通常写在#include 语句之后和 int main() 之前)
cout << ...
因为编译器按顺序读取代码。请注意,使用 'using' 被认为是不好的做法。我建议习惯使用 std:: 命名空间调用程序:
std::cout << "Hello World!" << std::endl;
解决您的具体问题:
#include <iostream>
using namespace std;
class podst { ...
我想练习一些基本的 C++ 知识。我专注于继承只是为了试验一下。一切顺利,直到我遇到一些奇怪的问题:'cout' 未在此范围内声明。我已经查看了一些主题,但在大多数主题上,提示就像追加库或写 'using namespace std' 但它没有解决我的问题。
#include <iostream>
class podst
{
public:
float a;
float b;
float dodaw();
podst(float c,float d) : a(c), b(d)
{
}
};
float podst::dodaw()
{
return (a+b);
}
class poch : public podst
{
poch() : podst(5,4)
{
cout << a << endl << b << dodaw() << endl;
}
};
using namespace std;
int main()
{
podst podst(1,2);
cout << podst.dodaw() << endl;
poch poch2;
return 0;
}
poch
从全局命名空间调用 cout
,而不是从 std
命名空间。请注意 using namespace std;
指令实际上在它下面,而不是上面。如果您不想每次都写整个命名空间前缀,最好将该指令放在每个函数的基础上,而不是每个文件的基础上:
poch() : podst(5,4)
{
using namespace std;
cout << a << endl << b << dodaw() << endl;
}
这种方法将有助于将大型代码库保持完整。
using namespace std 低于您第一次使用 cout 时的值。 尝试在 poch class.
中使用 std::cout 和 std::endlusing声明从你写的地方开始生效。由于对 cout
的调用高于该点,因此它不在 using
范围内,您必须在那里拼写 std::cout
。
You should avoid using namespace std
。一个更好的选择是只引入你需要的东西,例如using std::cout;
。但更好的选择是只使用限定名称。打字有点多,但可以防止歧义;在我所见过的每个代码库中,限定名称无处不在——至少对我来说——像 cout
这样的非限定名称总是有点令人惊讶,并且在阅读代码时会稍微绊倒我。
好吧,因为 poch 是 class 从父 class 继承的,你需要 在文件的开头声明标准命名空间,使其对所有 classes 和函数都是全局的,但是你在 main 之前声明了它,所以 poch class 无法使用它,而你可以已明确使用 std::cout 和 std::endl,程序编译不会有问题。
class poch : public podst
{
poch() : podst(5,4)
{
//you should have used std::cout
std::cout << a << endl << b << dodaw() << std::endl;
}
};
如果你想省略标准库命名空间调用者::std,你应该写成
using namespace std;
之前(通常写在#include 语句之后和 int main() 之前)
cout << ...
因为编译器按顺序读取代码。请注意,使用 'using' 被认为是不好的做法。我建议习惯使用 std:: 命名空间调用程序:
std::cout << "Hello World!" << std::endl;
解决您的具体问题:
#include <iostream>
using namespace std;
class podst { ...