Strange errors "hello world" program

Pages: 12
Dear all,

I only just started with C++ i downloaded the tutorial from this site and started writing. iam using winAVR-20100110.

As you can see i allready removed the #include <iostream> because this directive came up with an error. the file is named main.c and the makefile has been named main.

below the code as writen right now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  //My first program in C++



using namespace std; 



int main ()
{

	cout << "hello World!";
	return 0;
}


when i click "make all" the following happens:

> "make.exe" all

-------- begin --------
avr-gcc (WinAVR 20100110) 4.3.3
Copyright (C) 2008 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


Compiling C: main.c
avr-gcc -c -mmcu=atmega32 -I. -gdwarf-2 -DF_CPU=8000000UL -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Wstrict-prototypes -Wa,-adhlns=./main.lst -std=gnu99 -MMD -MP -MF .dep/main.o.d main.c -o main.o
main.c:4: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'namespace'
main.c:9: warning: function declaration isn't a prototype
main.c: In function 'main':
main.c:11: error: 'cout' undeclared (first use in this function)
main.c:11: error: (Each undeclared identifier is reported only once
main.c:11: error: for each function it appears in.)
make.exe: *** [main.o] Error 1

> Process Exit Code: 2
> Time Taken: 00:06


Due to my very low amount of experience iam not surten what could be the problem, and i hope you can help me out.

Kind Regards,
Maulus
1
2
3
#include <iostream> //Throw this up there.

using namespace std;


Edit: Should of read properly =/
Last edited on
i allready removed the #include <iostream> because this directive came up with an error.

But what was the error? Since that line is needed in this case (because you use cout), that's the problem which needs to be addressed.
closed account (z05DSL3A)
Compiling C: main.c

Could that be a big clue? It looks like you are using a C compiler...
I should want to stab Olysold:

http://www.urbandictionary.com/define.php?term=should%20of


Your compiler acts weird imho.

main.c:4: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'namespace'


Wait, why?
using namespace x; is a core language feature as far as I know.

Get a better compiler like microsoft's, clang, or GCC (MinGW on windows I think).

Also rename main.c to .cpp, some compilers (I heard) may apply a C compiler to .c files.

main.c:11: error: 'cout' undeclared (first use in this function)


Should really be accessible through <iostream>, how can it come up with an error?
Maybe changing the file name from main.c to main.cpp will cause it to be detected and compiled as c++. Or there may be some compiler option to switch between c and c++ ?
Last edited on
When main.c is renamed as main.cpp:

> "make.exe" all
make.exe: *** No rule to make target `all'. Stop.

> Process Exit Code: 2
> Time Taken: 00:00


When #include <iostream>:

> "make.exe" all
make.exe: *** No rule to make target `all'. Stop.

> Process Exit Code: 2
> Time Taken: 00:01

there is a drop down menu where i can pick from a whole list of...things
a few are:
plain text
c#
Css
D
Diff
Ada
C/C++ (this one is selected)

because the program comes back with make.exe i reckonned there might be something in the make file thats wrong?

Earlier i forgot to mention the .cpp file changed the icon it now contains the letters C++. as for the .c file the icon gives just C.
Last edited on
"make.exe" is a part of the compiler, no?

Try downloading Code::Blocks and MinGW as a bundle, you can get it at http://www.codeblocks.org/downloads
Then choose "Download the binary release"
Then choose "codeblocks-12.11mingw-setup.exe"

It's free and it should compile "Hello World" without problems
Last edited on
closed account (zb0S216C)
Grey Wolf wrote:
"Could that be a big clue? It looks like you are using a C compiler..."

S/he is. S/he is using AVR-GCC: a 3rd-party C library for use with GCC based on Atmel AVR micro-controllers.

@Maulus: You need to get rid of the compiler you're using now and replace it with a better compiler. May I recommend either GNU's GCC compiler, preferably one that supports C++, or Microsoft's compiler. Code::Blocks, and IDE, is shipped with GNU's C++-supporting compiler by default. Microsoft's Visual Express is shipped with their own compiler which also supports C++.

Code::Blocks: http://www.codeblocks.org/
Visual Express: http://www.microsoft.com/visualstudio/eng/products/visual-studio-express-for-windows-desktop

Wazzak
closed account (18hRX9L8)
You can also use Orwell Dev-Cpp. http://sourceforge.net/projects/orwelldevcpp/files/Portable%20Releases/Dev-Cpp%205.4.1%20MinGW%204.7.2%20Portable.7z/download
i want to thank you all for the fast and helpfull replys after downloading and installing code::blocks and Orwell Dev-Cpp and implementing the "hello world" program they both come with the same problems:

error: unknown type name 'using'
error: expected '=', ',', ';', 'asm' or '__attribute__' before 'std'
error: 'cout' undeclared (first use in this function)

main.c:3:20: fatal error: iostream: No such file or directory

i think i'am doing something wrong, i hope you guys are able to help me out!

i allso read somewhere that the iostream header has become obselete with the new compilers, this could be the reason why iam unable to find it anywhere.

Thanks,

Maulus
Last edited on
What's your compiler?

in C::B:
Settings -> Compiler -> Selected Compiler
GNU GCC compiler
Honestly, just go download code::blocks with minigw, it will solve all this and you can concentrate on learning the details of compilers later when you understand the language better.
thats the one i installed.
Maulus wrote:
error: expected '=', ',', ';', 'asm' or '__attribute__' before 'std'
This is the same error you got before, isn't it? I think Code::Blocks has mixed up the compilers.

Try uninstalling your compiler (the one you mentioned earlier - WinAVR), then re-installing Code::Blocks with MinGW. (or have you already done that?)

Afterwards, go to Settings -> Compiler and click the "Reset defaults" button on the top right. It should sort itself out.
Last edited on
i just uninstalled all compiler's including Code::blocks, made sure every single visual file has been removed.

reinstalled code::blocks 12.11 minGW

compiler = GNU GCC compiler.
(did aswell reset to default)

and yet again exactly the same error's, its starting to piss me off now to be honest, it can't be that hard right?!
closed account (zb0S216C)
Have you tried following Code::Blocks' Wiki page[1]?

[1]http://wiki.codeblocks.org/index.php?title=Creating_a_new_project

Wazzak
And does the code you are compiling look exactly like this?
1
2
3
4
5
6
7
8
9
10
// my first program in C++

#include <iostream>
using namespace std;

int main ()
{
  cout << "Hello World!";
  return 0;
}
I have been able to reproduce the problem by creating a file called main.c with code Chervil posted.
You really need to use a C++ compiler and not a C compiler. Are you compiling manually? Just use the little gear (compile) button in Code Blocks.
Pages: 12