This question already has answers here:
error C2065: 'cout' : undeclared identifier
(26 answers)
Closed 6 years ago.
So i'm trying to run this in Visual Studio but it gives me several errors in the output console:
1>------ Build started: Project: ConsoleApplication1, Configuration: Release x64 ------
1> stdafx.cpp
1> ConsoleApplication1.cpp
1>ConsoleApplication1.cpp(6): error C2039: 'cout': is not a member of 'std'
1> predefined C++ types (compiler internal)(209): note: see declaration of 'std'
1>ConsoleApplication1.cpp(6): error C2065: 'cout': undeclared identifier
1>ConsoleApplication1.cpp(6): warning C4554: '<<': check operator precedence for possible error; use parentheses to clarify precedence
1>ConsoleApplication1.cpp(6): error C2039: 'endl': is not a member of 'std'
1> predefined C++ types (compiler internal)(209): note: see declaration of 'std'
1>ConsoleApplication1.cpp(6): error C2065: 'endl': undeclared identifier
My code is as follows:
#include <iostream>
#include "stdafx.h"
void test(int x, int y)
{
std::cout << x + y << std::endl;
}
int main()
{
test(1, 2);
return 0;
}
You didn't read the documentation for the program you're using.
#include "stdafx.h" must come first.
Change the order of the header file inclusions. The precompiled header inclusion must always be the first non-comment in your source files.
Related
This question already has answers here:
Is bool a native C type?
(12 answers)
Closed 6 years ago.
Why am I getting a syntax error for my C header declaration?
Here is my header file, viterbi.h:
#ifndef VITERBI_H
#define VITERBI_H
void vitdec(float* , int , int , bool* );
#endif //VITERBI_H
And here is my implementation file, viterbi.c:
// viterbi.c : Defines the entry point for the console application.
//
#include "viterbi.h"
#include "math.h"
//void vitdec(float* sd, int frameLen, int rate, bool* hd);
void vitdec(float* sd, int frameLen, int rate, bool* hd)
{
//... The rest of the function
The errors from the Visual Studio 2010 compiler read:
viterbi.h(4): error C2143: syntax error : missing ')' before '*'
viterbi.h(4): error C2081: 'bool' : name in formal parameter list illegal
viterbi.h(4): error C2143: syntax error : missing '{' before '*'
viterbi.h(4): error C2059: syntax error : ')'
viterbi.h(4): error C2059: syntax error : ';'
viterbi.c(7): error C2065: 'bool' : undeclared identifier
viterbi.c(7): error C2065: 'hd' : undeclared identifier
viterbi.c(7): warning C4552: '*' : operator has no effect; expected operator with side-effect
As far as I have seen/can tell, this is valid syntax for a C declaration. If I compile viterbi.c as C++ code (viterbi.cpp), then the errors disappear. What is the syntax error?
bool is not a native C type, but for those using C99, try adding the line #include <stdbool.h>, which contains a macro that defines bool.
Since the C compiler in all Visual Studio/MSVC products uses C89, bool is not defined at all for you, as a native C type or otherwise. Workarounds include using typedef or enum to define bool. Examples are in the below link.
For more information, see: Is bool a native C type?
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
EDIT: You need to make a vector like this:
vector myVector(5);
not like this:
vector myVector[5];
Instead of vector<team> players[5]; vector<team> players(5); has to be. With this operation you'll create a vector of 5 players. In your code 5 empty vectors are created.
You should get a lot more errors, actually. I get the following:
cl /nologo /EHsc /Za /W4 stackoverflow.cpp
stackoverflow.cpp
stackoverflow.cpp(5) : error C2146: syntax error : missing ';' before identifier 'name'
stackoverflow.cpp(5) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
stackoverflow.cpp(9) : error C2061: syntax error : identifier 'string'
stackoverflow.cpp(10) : error C2146: syntax error : missing ';' before identifier 'getName'
stackoverflow.cpp(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
stackoverflow.cpp(10) : warning C4183: 'getName': missing return type; assumed to be a member function returning 'int'
stackoverflow.cpp(22) : error C2511: 'void team::setName(std::string)' : overloaded member function not found in 'team'
stackoverflow.cpp(3) : see declaration of 'team'
stackoverflow.cpp(23) : error C2065: 'name' : undeclared identifier
stackoverflow.cpp(23) : error C2065: 'b' : undeclared identifier
stackoverflow.cpp(30) : error C2556: 'std::string team::getName(void)' : overloaded function differs only by return type from 'int team::getName(void
stackoverflow.cpp(10) : see declaration of 'team::getName'
stackoverflow.cpp(30) : error C2371: 'team::getName' : redefinition; different basic types
stackoverflow.cpp(10) : see declaration of 'team::getName'
stackoverflow.cpp(31) : error C2065: 'name' : undeclared identifier
stackoverflow.cpp(42) : error C2039: 'setRuns' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
with
[
_Ty=team
]
stackoverflow.cpp(43) : error C2039: 'setName' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
with
[
_Ty=team
]
stackoverflow.cpp(45) : error C2039: 'getName' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
with
[
_Ty=team
]
stackoverflow.cpp(45) : error C2039: 'getRuns' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
with
[
_Ty=team
]
Class names of the C++ standard library are prefixed with std::. That's just part of their name. It is good practice to just always use the full name. In particular, it is very bad practice to use using namespace std; at global scope in a header file.
So let's remove using namespace std and write std:: everywhere.
team.h:
#include<string>
class team {
std::string name;
int runs;
public:
void setName(std::string a);
std::string getName();
void setRuns(int b);
int getRuns();
};
team.cpp:
#include<string>
#include "team.h"
void team::setRuns(int b) {
runs=b;
}
void team::setName(std::string a) {
name=b;
}
int team::getRuns() {
return runs;
}
std::string team::getName() {
return name;
}
main.cpp:
#include<iostream>
#include<vector>
#include<cstdio>
#include "team.h"
int main() {
std::vector<team> players[5];
players[0].setRuns(10);
players[0].setName("Michael");
printf("%s %d",players[0].getName(),players[0].getRuns());
system("pause");
return 0;
}
This removes most errors:
stackoverflow.cpp(22) : error C2065: 'b' : undeclared identifier
stackoverflow.cpp(39) : error C2039: 'setRuns' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
with
[
_Ty=team
]
stackoverflow.cpp(40) : error C2039: 'setName' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
with
[
_Ty=team
]
stackoverflow.cpp(42) : error C2039: 'getName' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
with
[
_Ty=team
]
stackoverflow.cpp(42) : error C2039: 'getRuns' : is not a member of 'std::vector<team,std::allocator<_Ty>>'
with
[
_Ty=team
]
The a in setRuns is certainly a typo. We'll also fix that. I'll also just remove the unnecessary system("pause");. Now we have code which exhibits only the error you asked about.
Let's take a closer look at the following line:
std::vector<team> players[5]
I think the misunderstanding here is that [5] specifies the size of the std::vector. This is a misconception. A std::vector has no fixed size and will start at 0 elements by default. It does not need any [] syntax for initialization. The [] syntax here denotes an array. An array is a fixed-size collection of elements.
So what you created here is an array of 5 vectors. Apparently, that's not at all what you wanted. Just replace the [5] with (5) to get the meaning of "vector that starts with 5 elements":
std::vector<team> players(5);
Now it compiles. But it will probably crash at run-time, because you also use printf incorrectly:
printf("%s %d",players[0].getName(),players[0].getRuns());
printf is a C function which was designed long before C++ existed. %s means that a C-style string is expected. You could provide one like this:
printf("%s %d",players[0].getName().c_str(),players[0].getRuns());
Or you just use C++ streams:
std::cout << players[0].getName() << " " << players[0].getRuns();
I googled the error lines of my problem and got hardly any hits and I don't speak russian. I also found this, but it doesn't seem to help me.
This is my code
#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
int main() {
boost::asio::io_service io;
boost::asio::deadline_timer t(io,boost::posix_time::seconds(5));
t.wait();
std::cout << "Hello World" << std::endl;
return 0;
}
These be my error messages:
1>------ Build started: Project: ConsoleApplication1, Configuration: Debug Win32 ------
1> Main.cpp
1>c:\sdk\boost\asio\detail\impl\win_thread.ipp(52): error C2039: 'QueueUserAPC' : is not a member of '`global namespace''
1>c:\sdk\boost\asio\detail\impl\win_thread.ipp(52): error C3861: 'QueueUserAPC': identifier not found
1>c:\sdk\boost\asio\detail\impl\win_object_handle_service.ipp(374): error C3861: 'RegisterWaitForSingleObject': identifier not found
1>c:\sdk\boost\asio\detail\impl\win_object_handle_service.ipp(416): error C3861: 'RegisterWaitForSingleObject': identifier not found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Any insights would be appreciated, thanks!
EDIT
Adding #include <Windows.h> before the headers changed the error messages to
1>c:\program files (x86)\windows kits\8.0\include\um\prsht.h(607): error C2146: syntax error : missing ';' before identifier 'hdr'
1>c:\program files (x86)\windows kits\8.0\include\um\prsht.h(607): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\windows kits\8.0\include\um\windows.h(247): warning C4193: #pragma warning(pop) : no matching '#pragma warning(push)'
1>c:\sdk\boost\asio\detail\socket_types.hpp(22): fatal error C1189: #error : WinSock.h has already been included
prsht.h:
tracking that down lead me to find this:
typedef struct _PSHNOTIFY
{
NMHDR hdr; //line 607 NMHDR is undefined.
LPARAM lParam;
} PSHNOTIFY, *LPPSHNOTIFY;
Not real sure where to go from here.
I am setting up some framework for a little 2D game. Right now, I just have a few classes, but I am immediately falling into compiler problems.
I have run this program with only the main function, so I can confirm that the Allegro (graphics library) library linking has worked.
I have put all my header files (.h) under the "Header Files" section in the Solution explorer and all my source (.cpp) in the "Source Files" section. I have only modified settings from default as according to this Allegro tutorial: http://wiki.allegro.cc/index.php?title=Windows,_Visual_Studio_2010_and_Allegro_5. It should not have affected anything in a destructive way.
I have several files, so I will list each one. I will skip some code to reduce size and redundancy. When I omit any code or do anything not verbatim, I will put a comment in the code saying so. (EDIT: I actually did not skip any code)
main.cpp
#include "common.h"
int main(int argc, char **argv)
{
ALLEGRO_DISPLAY *display = NULL;
World* world = new World(640, 480);
if(!al_init()) {
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}
display = al_create_display(640, 480);
if(!display) {
fprintf(stderr, "failed to create display!\n");
return -1;
}
al_clear_to_color(al_map_rgb(0,0,0));
world->draw(display);
al_flip_display();
al_rest(10.0);
al_destroy_display(display);
return 0;
}
common.h
#if !defined(COMMON_INC)
#define COMMON_INC
#include "World.h"
#include "Pane.h"
#include <stdio.h>
#include <allegro5/allegro.h>
#endif
World.h
#if !defined(WORLD_INC)
#define WORLD_INC
#include "common.h"
#include "Pane.h"
class World{
public:
World(int wp, int hp);
void draw(ALLEGRO_DISPLAY* display);
void update();
protected:
private:
Pane* panel;
int heightPix, widthPix;
};
#endif
World.cpp
#include "common.h"
World::World(int wp, int hp){
widthPix = wp;
heightPix = hp;
panel = new Pane(this, 10, 10, 300, 400);
return;
}
void World::draw(ALLEGRO_DISPLAY* display){
panel->draw(display);
return;
}
Pane.h
#if !defined(PANE_INC)
#define PANE_INC
#include "common.h"
class Pane{
public:
Pane(World* w, int xv, int yv, int wi, int he);
World* getWorld();
int getZ();
void draw(ALLEGRO_DISPLAY* display);
ALLEGRO_BITMAP* getBackground();
protected:
void setXY(int xv, int yv);
void setWidthHeight(int wi, int he);
void setZ(int zv);
void setBackground(ALLEGRO_BITMAP* ba);
private:
int z;
int x, y; //pixels
int width, height; //pixels
World* world;
ALLEGRO_BITMAP* background;
};
#endif
Pane.cpp
#include "common.h"
Pane::Pane(World* w, int xv, int yv, int wi, int he){
this->setWidthHeight(wi, he);
this->setXY(xv, yv);
this->world = w;
}
World* Pane::getWorld(){
return world;
}
void Pane::setXY(int xv, int yv){
x = xv;
y = yv;
return;
}
void Pane::setWidthHeight(int wi, int he){\
width = wi;
height = he;
return;
}
void Pane::setZ(int zv){
z = zv;
return;
}
void Pane::draw(ALLEGRO_DISPLAY* display){
if(background != NULL)
al_draw_bitmap(background, x, y, 0);
else{
background = al_create_bitmap(width, height);
al_set_target_bitmap(background);
al_clear_to_color(al_map_rgb(255, 0, 255));
al_set_target_bitmap(al_get_backbuffer(display));
al_draw_bitmap(background, x, y, 0);
}
return;
}
The compiler produces this error report upon building: (I called the game madscientist because it is supposed to be alchemy-themed)
1>------ Build started: Project: MadScientist, Configuration: Debug Win32 ------
1> main.cpp
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(8): error C2061: syntax error : identifier 'World'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): warning C4183: 'getWorld': missing return type; assumed to be a member function returning 'int'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(11): error C2061: syntax error : identifier 'ALLEGRO_DISPLAY'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): warning C4183: 'getBackground': missing return type; assumed to be a member function returning 'int'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(18): error C2061: syntax error : identifier 'ALLEGRO_BITMAP'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(24): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(24): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(10): error C2061: syntax error : identifier 'ALLEGRO_DISPLAY'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\main.cpp(22): error C2660: 'World::draw' : function does not take 1 arguments
1> World.cpp
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(8): error C2061: syntax error : identifier 'World'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): warning C4183: 'getWorld': missing return type; assumed to be a member function returning 'int'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(11): error C2061: syntax error : identifier 'ALLEGRO_DISPLAY'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): warning C4183: 'getBackground': missing return type; assumed to be a member function returning 'int'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(18): error C2061: syntax error : identifier 'ALLEGRO_BITMAP'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(24): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(24): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(10): error C2061: syntax error : identifier 'ALLEGRO_DISPLAY'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.cpp(6): error C2661: 'Pane::Pane' : no overloaded function takes 5 arguments
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.cpp(10): error C2511: 'void World::draw(ALLEGRO_DISPLAY *)' : overloaded member function not found in 'World'
1> c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(7) : see declaration of 'World'
1> Pane.cpp
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(8): error C2061: syntax error : identifier 'World'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): warning C4183: 'getWorld': missing return type; assumed to be a member function returning 'int'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(11): error C2061: syntax error : identifier 'ALLEGRO_DISPLAY'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): warning C4183: 'getBackground': missing return type; assumed to be a member function returning 'int'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(18): error C2061: syntax error : identifier 'ALLEGRO_BITMAP'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(24): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(24): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(10): error C2061: syntax error : identifier 'ALLEGRO_DISPLAY'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.cpp(3): error C2511: 'Pane::Pane(World *,int,int,int,int)' : overloaded member function not found in 'Pane'
1> c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(6) : see declaration of 'Pane'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.cpp(9): error C2556: 'World *Pane::getWorld(void)' : overloaded function differs only by return type from 'int *Pane::getWorld(void)'
1> c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9) : see declaration of 'Pane::getWorld'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.cpp(9): error C2371: 'Pane::getWorld' : redefinition; different basic types
1> c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9) : see declaration of 'Pane::getWorld'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.cpp(10): error C2065: 'world' : undeclared identifier
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.cpp(30): error C2511: 'void Pane::draw(ALLEGRO_DISPLAY *)' : overloaded member function not found in 'Pane'
1> c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(6) : see declaration of 'Pane'
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
You have all of the info that I do. I will summarize it for you to save the trouble of looking through everything.
There are classes separated into header interfaces and source implementations
common.h is included by all files. It itself includes all of the class definitions in the other header files
When compiled, the compiler does not recognize classes defined in other header files as data types. Errors, as you would expect, cascade down.
The real-time error checker does not register any errors. When hovering over the word "World" when it is used as a datatype in Pane, for example, it recognizes the type perfectly. The real-time error checker is the feature that red underlines errors before compiler time.
This Visual Studio 2012 Express is new except for the modifications mentioned earlier. The project was created as an empty C++ project. Before many headers and sources were add, the main function compiled correctly.
Thank you for any responses. If you have any questions, please ask. The problems I have with Visual Studio always irk me.
EDITS:
My circular header logic is guarded by the header guards. I don't think this redundancy is a problem.
I tried removing all includes from my header files except for includes to Allegro libraries. This seemed to work better, but there are still weird problems. Why the act of including is causing these data types to error is still a mystery. Here is the new error log:
1>------ Build started: Project: MadScientist, Configuration: Debug Win32 ------
1> main.cpp
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(16): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(16): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1> World.cpp
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(16): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(16): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.cpp(6): error C2065: 'panel' : undeclared identifier
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.cpp(11): error C2065: 'panel' : undeclared identifier
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.cpp(11): error C2227: left of '->draw' must point to class/struct/union/generic type
1> type is ''unknown-type''
1> Pane.cpp
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(16): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(16): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
EDITS ROUND 2:
I switched my code around so the common.h only includes stdio and Allegro. All the header and source files include common.h and then any class's header file that they are using individually. Pane.cpp include Pane.h and common.h. World.h includes Pane.h and common.h.
Error log reads:
1>------ Build started: Project: MadScientist, Configuration: Debug Win32 ------
1> main.cpp
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): error C2061: syntax error : identifier 'World'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(10): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(10): warning C4183: 'getWorld': missing return type; assumed to be a member function returning 'int'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1> World.cpp
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): error C2061: syntax error : identifier 'World'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(10): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(10): warning C4183: 'getWorld': missing return type; assumed to be a member function returning 'int'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.cpp(7): error C2661: 'Pane::Pane' : no overloaded function takes 5 arguments
1> Pane.cpp
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(16): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(16): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
This is how I see my include order now:
main includes common
common defines COMMON_INC
common includes stdio and allegro
main includes World.h
World defines WORLD_INC
World includes common, which is blocked. But this should be okay because common is already included
World includes Pane
Pane defines PANE_INC
Pane includes common, which is blocked. But this should be okay because common is already included.
Pane includes World. This is blocked because main already included World. Shouldn't this be okay since World was already included. Why do I even have compiler guards if I need to include files many times over, once for each file that uses it?
EDITS ROUND 3:
I have learned a lot from the answers and comments here. It turns out that, according to Wikipedia, "circular dependencies are often introduced by inexperienced programmers who need to implement some kind of callback functionality." The mere fact that Pane uses World and World uses Pane is a design flaw. In Java, this was all fine for me. It was very easy; I thought that this was the best way to notify the entity that "possesses" an object of the object's actions.
It turns out that this is a bad design scheme. Objects should not have dependencies to their "possessors". Instead, I need to implement a observer system, where the Pane can tell the World that it has updated its state.
Reading wikipedia cleared up any questions that I had, so I now consider this question finished. Thanks to the contributors for putting up for a learning programmer.
You header files cannot be possibly linked correctly. You included your headers in circular fashion. Your header files include common.h, while common.h in turn includes other headers, like World.h and Pane.h.
This is not compilable by any compiler. You have to develop a hierarchy of headers, from low-level headers to high-level headers and make sure that higher-level headers include only lower-level headers. That way you will make sure you have no circular inclusions.
Anyway, why does your common.h include World.h and Pane.h? This looks like an obvious error. common.h is intended to be a low-level header. Let World.h and Pane.h include it, but don't include World.h and Pane.h into common.h.
Note, that header guards do not solve anything in this case. They simply make sure that the inclusion cycle will not get infinite. They break the cycle, but they don't help you to resolve circular dependencies between declarations, if such dependencies exist.
Look what happens in your case when processing main.cpp
main.cpp includes common.h
common.h defines COMMON_INC
common.h includes World.h
World.h defines WORLD_INC
World.h includes common.h. The whole common.h is skipped by include guards, because COMMON_INC got defined at step 2.
World.h includes Pane.h
Pane.h defines PANE_INC
Pane.h includes common.h. The whole common.h is skipped by include guards, because COMMON_INC got defined at step 2.
Pane.h referred to type World. Type World is unknown, since we haven't gotten to its definition in World.h yet. Error!
I think everything will compile even with the circular includes, if you make the following changes
In common.h you need to rearrange order of headers.
#include <allegro5/allegro.h>
#include "World.h"
#include "Pane.h"
#include <stdio.h>
In pane.h, after the #include common.h, add a forward declaration for class World
#include "common.h"
class World;
class Pane{
.....
I think this will make your errors disappear or atleast decrease them substantially.
My answer is based on the original code you put up, not on the Edit.
I am attempting to print containers, like sets and maps. The book I am using says the following code is valid:
#include <iostream>
#include <set>
#include <iterator>
#include <fstream>
using namespace std;
template <typename Container>
void print (const Container & c, ostream & out = cout)
{
typename Container::const_iterator itr;
for( itr=c.begin(); itr!=c.end(); ++itr)
out << *itr << " ";
out << endl;
}
int main()
{
ifstream fin("Test.txt");
set<string> s( istream_iterator<string>(fin),
istream_iterator<string>() );
print( s );
return 0;
}
Yet I am getting the an error from Visual Studio's compiler. What am I missing? I know it is likely something simple like an include yet I am unfamiliar with STL Containers and C++ iterators.
I already have #include <iterator>
Errors:
'Container': must be a class or namespace when followed by '::'
'itr' : undeclared identifier
'const_iterator' : is not a member of '`global namespace''
and a few more I am sure are a result of the first one.
Edit:
Per the textbook, the following code should be equivalent to that in my main. I could not get it to work but it may help:
ifstream fin("Test.txt");
string x;
set<string> s;
while( fin >> x)
s.insert(x);
Edit:
Visual Studio build output:
------ Build started: Project: project, Configuration: Debug Win32 ------
Build started 4/15/2012 1:19:25 PM.
InitializeBuildStatus:
Touching "Debug\project.unsuccessfulbuild".
ClCompile:
Project4.cpp
c:\users\admin\documents\visual studio 2010\projects\project\project\project4.cpp(11): error C2825: 'Container': must be a class or namespace when followed by '::'
c:\users\admin\documents\visual studio 2010\projects\project\project\project4.cpp(22) : see reference to function template instantiation 'void print<std::set<_Kty>
(std::istream_iterator<_Ty>,std::istream_iterator<_Ty> (__cdecl *)(void))>(Container (__cdecl &),std::ostream &)' being compiled
with
[
_Kty=std::string,
_Ty=std::string,
Container=std::set<std::string> (std::istream_iterator<std::string>,std::istream_iterator<std::string> (__cdecl *)(void))
]
c:\users\admin\documents\visual studio 2010\projects\project\project\project4.cpp(11): error C2039: 'const_iterator' : is not a member of '`global namespace''
c:\users\admin\documents\visual studio 2010\projects\project\project\project4.cpp(11): error C2146: syntax error : missing ';' before identifier 'itr'
c:\users\admin\documents\visual studio 2010\projects\project\project\project4.cpp(11): error C2065: 'itr' : undeclared identifier
c:\users\admin\documents\visual studio 2010\projects\project\project\project4.cpp(12): error C2065: 'itr' : undeclared identifier
c:\users\admin\documents\visual studio 2010\projects\project\project\project4.cpp(12): error C2228: left of '.begin' must have class/struct/union
type is 'std::set<_Kty> (__cdecl &)'
with
[
_Kty=std::string
]
c:\users\admin\documents\visual studio 2010\projects\project\project\project4.cpp(12): error C2065: 'itr' : undeclared identifier
c:\users\admin\documents\visual studio 2010\projects\project\project\project4.cpp(12): error C2228: left of '.end' must have class/struct/union
type is 'std::set<_Kty> (__cdecl &)'
with
[
_Kty=std::string
]
c:\users\admin\documents\visual studio 2010\projects\project\project\project4.cpp(12): error C2065: 'itr' : undeclared identifier
c:\users\admin\documents\visual studio 2010\projects\project\project\project4.cpp(13): error C2065: 'itr' : undeclared identifier
Build FAILED.
Time Elapsed 00:00:01.00
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
It thinks this is a function declaration:
set<string> s( istream_iterator<string>(fin),
istream_iterator<string>() );
Adding an extra pair of parentheses will correct it:
set<string> s( (istream_iterator<string>(fin)),
istream_iterator<string>() );
There are many examples of this on SO if you search for Most Vexing Parse.
Edit: You also need to add #include <string>
Seems like the compilers (also clang++) seem to get confused. The following works for me, though:
#include <iostream>
#include <set>
#include <iterator>
#include <fstream>
using namespace std;
template <typename Container>
void print (const Container & c, ostream & out = cout)
{
typename Container::const_iterator itr;
for( itr=c.begin(); itr!=c.end(); ++itr)
out << *itr << " ";
out << endl;
}
int main()
{
ifstream fin("Test.txt");
istream_iterator<string> first(fin), last;
set<string> s(first, last);
print(s);
return 0;
}
However, I would recommend turning your print function into something which accepts an iterator range which which you output using std::copy, as is shown here: http://www.sgi.com/tech/stl/ostream_iterator.html