疯狂的基本 c++错误

发布于 2024-11-08 05:37:37 字数 607 浏览 0 评论 0原文

谁能解释为什么 Codeblocks 给我这些错误?:

error: ISO C++ forbids declaration of 'cout' with no type
error: invalid use of '::'
error: expected ';' before '<<' token
error: '<<x>>' cannot appear in a constant-expression      // <<x>> is many different variable names

我的代码实际上很简单:

#include <iostream>
#include "myclass.h"

int main(){
   std::string data;
   std::string e;

   e = myclass().run(data);
   std::cout << e << std::endl;

   return 0;
}

世界上发生了什么?

编辑:是的,我确实有 iostream。抱歉没有早点把它放在那里

Can anyone explain why Codeblocks is giving me these errors?:

error: ISO C++ forbids declaration of 'cout' with no type
error: invalid use of '::'
error: expected ';' before '<<' token
error: '<<x>>' cannot appear in a constant-expression      // <<x>> is many different variable names

my code is literally as simple as:

#include <iostream>
#include "myclass.h"

int main(){
   std::string data;
   std::string e;

   e = myclass().run(data);
   std::cout << e << std::endl;

   return 0;
}

what in the world is going on?

EDIT: and yes, i do have iostream. sorry for not putting it there earlier

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

逆流 2024-11-15 05:37:37

添加

#include <iostream>

std::cout 在此标头内


编辑:关于您的编辑 - 这意味着问题肯定在 myclass.h 内,或者有一些代码,但不是此处显示。

Add

#include <iostream>

std::cout is inside this header


EDIT: regarding your edit - this means, that the problem is for sure inside myclass.h or there's some code, that is not shown here.

请止步禁区 2024-11-15 05:37:37

#include 怎么样?

没有它(和以下代码)

#include <iostream>

int main(){
   std::string data;
   std::string e;

   std::cout << e << std::endl;

   return 0;
}

我的 g++ 报告:

tst.cpp: In function `int main()':
tst.cpp:4: undeclared variable `string' (first use here)
tst.cpp:4: parse error before `;'
tst.cpp:5: parse error before `;'
tst.cpp:7: `e' undeclared (first use this function)
tst.cpp:7: (Each undeclared identifier is reported only once
tst.cpp:7: for each function it appears in.)

How about #include <string>?

Without it (and the following code)

#include <iostream>

int main(){
   std::string data;
   std::string e;

   std::cout << e << std::endl;

   return 0;
}

my g++ reports :

tst.cpp: In function `int main()':
tst.cpp:4: undeclared variable `string' (first use here)
tst.cpp:4: parse error before `;'
tst.cpp:5: parse error before `;'
tst.cpp:7: `e' undeclared (first use this function)
tst.cpp:7: (Each undeclared identifier is reported only once
tst.cpp:7: for each function it appears in.)
何时共饮酒 2024-11-15 05:37:37

您应该包含

you should include <iostream>

凝望流年 2024-11-15 05:37:37

您是否在某处包含了

知道您已添加 后进行编辑

,您可以检查:

  • #include
  • 如果您的类定义已完成分号

如果一切正常我想检查你的 myclass.h :-(

Did you include <iostream> somewhere?

EDIT after knowing that you have added <iostream>

Well you can check:

  • #include <string>
  • If your class definition is finished by semi-colon

If everything is OK I want to check your myclass.h :-(

伴梦长久 2024-11-15 05:37:37

您发布的代码(带有编辑)是正确的。一定有
myclass.h 中发生了一些有趣的事情。 (也许是一个

#define std

,以便编译器看到 ::cout。)

您可能想查看预处理器输出:
Unix 下的编译器选项 -E,Visual Studio 下的 /E。它
将会很长,但您感兴趣的只是最后 10 条
左右线;预处理器对您的代码做了什么。

The code you post (with the EDIT) is correct. There must be
something funny going on in myclass.h. (Maybe a

#define std

, so that the compiler sees ::cout.)

You might want to have a look at the pre-processor output:
compiler option -E under Unix, /E for Visual Studios. It
will be voluminous, but all you're interested in is the last 10
or so lines; what the pre-processor has done to your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文