calccrypto
calccrypto

Reputation: 8991

Crazy basic c++ errors

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

Upvotes: 1

Views: 795

Answers (5)

Mayank
Mayank

Reputation: 5728

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 :-(

Upvotes: 2

Kiril Kirov
Kiril Kirov

Reputation: 38163

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.

Upvotes: 11

KevinDTimm
KevinDTimm

Reputation: 14376

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.)

Upvotes: 3

James Kanze
James Kanze

Reputation: 153909

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.

Upvotes: 1

Andre Holzner
Andre Holzner

Reputation: 18675

you should include <iostream>

Upvotes: 2

Related Questions