Error LNK 2005 Visual Studio 2008 - c++

Please help to build my project, i gave up after 2nd hour.
That is how it looks like:
BrickClass.h
- #include"header.h"
header.h
- #pragma once
- #include windows.h
- #include windowsX.h
- #include tchar.h
- #include commctrl.h
- #include "matrixClass.h"
- #include "resource.h"
mainClass.h
- #include "header.h"
- #include "brickClass.h"
matrixClass.h
- #include cstdlib
- #include cstdio
- #include math.h
brickClass.cpp
- #include "brickClass.h"
main.cpp
- #include "mainClass.h"
mianClass.cpp
- #include "mainClass.h"
What i need to do to make him happy?I was tried a lot of variants but cant figure out HOW..?
Appreciate your help.
The project is Here : http://www.filehosting.org/file/details/381812/Tetris.rar

If you're giving up after the 2nd hour, this probably isn't the right industry for you :-) There'll be times when you'll have spent days trying to solve a problem, kicking yourself at the end because it was so simple in retrospect.
Anyway, on to the problem at hand. It's almost certainly because you have code in your BrickClass header file.
By including that header file in both main.cpp (via mainClass.h) and BrickClass.cpp, each of the object files gets an independent copy of the code.
Then, when you try to link those object files together, the linker finds that there are two copies.
Header files should generally contain declarations, like extern int i; or function prototypes such as int xyzzy (void);.
The definitions, like int i; and functions such as int xyzzy (void) {return 42;}, should only be placed in the "regular" source files.

Related

After defining a function in another file, visual studio does not recognize the function

I'm creating windows console app that has many pages (split in files). I'm facing a problem when executing the program and Visual Studio throws 'startpage' identifier not found error (startpage is the function and the file name is startpage.h)
I've tried using:
external int startpage(); and
int startpage();.
I've tried changing only the function name too (so the file and function don't use the same name).
I have no idea why this is happening. Other files with different functions are working. The file "startpage.h" uses two functions defined in other files, and those are not throwing any errors.
#include "include/startpage.h"
#include <iostream>
#include <conio.h>
#include "include/concol.h"
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
startpage();
}
```
Here is the error:
Error code: C3861: 'startpage' identifier not found
Move #include "pch.h" to the top. The compiler ignores everything above the inclusion of precompiled header. – Igor Tandetnik
This worked!
Thank you so much guys!

Why does changing the order of including psapi.h gives compilation erros?(Indentifier BOOL is undefined)

I am using Visual Studio Community 2017 to code c++. When I run the following code everything works fine.
#include "pch.h"
#include<Windows.h>
#include<Psapi.h>
#include <iostream>
#include <conio.h>
int main()
{
std::cout << "Really!! How do you do it?";
_getch();
}
But if I change the order of #includes by including psapi.h before Windows.h, compiler goes badass and throws 198 errors at me, which surprisingly(maybe only to me) includes Identifier "BOOL" is undefined.
Why is this happening?
Since Psapi.h's include tree is trivial, I'm going to exemplify. Everything relies on VStudio 2015 (Community) (v14.0.25431.01 Update 3) and Windows Kits 8.1 (? funny, because v10 is there too) files (with default env vars and preprocessor definitions):
BOOL is defined in minwindef.h (#157: typedef int BOOL;)
Psapi.h only includes one file (#27: #include <winapifamily.h>)
winapifamily.h doesn't include any other file
So, when reaching Psapi.h (#87: BOOL WINAPI EnumProcesses (...), the compiler doesn't know anything about BOOL, so it complains.
Windows.h includes minwindef.h (indirectly, via windef.h), and that's why it works when you include it before Psapi.h.
Personally, I think it's a bug in Psapi.h, since it's not self contained, but there might be a good reason (that I'm not aware of) for that. Anyway, if this is indeed a bug, it wouldn't be MS's 1st one :)
#include <Windows.h>
#include <WinSock2.h>
// main present just for rigorosity's sake
int main() {
return 0;
}
to answer the question, I know this is DATED but the issues persist today. You need the following:
#include "stdafx.h"
#include <stdbool.h>
#include <Windows.h>
#include <stdlib.h>
#include <psapi.h>
After stdlib.h was included, the errors were gone.

Same name headers with different content in Ubuntu system

I have two c++ projects in my Eclipse that uses same function gettimeofday(). First project compiles fine while other reports error
error: ‘gettimeofday’ was not declared in this scope
By presing F3 on function name I found that in first project case I was navigated to /usr/include/86_64-linux-gnu/sys/time.h. Second project also has #include <time.h>, but F3 navigates to sys/time.h, which has no ‘gettimeofday’ function.
Why same #include <time.h> navigates to different folders?
Why Linux has two same names includes at all?
Why both includes #include <time.h> are different?
UPD
I found that /sys/timeb.h has inclusion #include <time.h> which navigates to /usr/lib/time.h. I was expecting /sys/timeb.h will include /sys/timeb.h. How Eclipse and GCC decides to go/usr/lib/time.hor/sys/time.h` ?
Why "professional" /sys/timeb.h leaves ambiguity in code just by making #include <time.h> instead defining more exact path? I suppose <time.h> might point to both - <sys/time.h> and C <time.h>
On Linux, the function int gettimeofday(struct timeval *tv, struct timezone *tz); is defined in #include <sys/time.h>. When using it in your code you should include it exactly like that. The other time.h is the standard C library that contains time structures and functions such as time_t and mktime. When you are using this file in a C++ program you should include it as #include <ctime> which should help you avoid this type of error in the future.

C++ cout gives undeclared identifier

So, I have this question. Why does cout throws
error C2065: 'cout' : undeclared identifier
I am using Visual Studio 2012 as an IDE and I am writing a school project. I have everything done except an example file. So I am trying to write something on the screen like this:
#include "iostream"
#include "stdafx.h"
using namespace std;
int main()
{
cout<<"example";
return 0;
}
So the problem is with cout... printf works fine, but I want to use cout.
EDIT:
I've changed "" to <> but it is not helping. Also I am using this code only for example... This is not the whole project.
stdafx.h shall be the first include directive in your source file.
Switch files and convert the second include to <>, as other suggested.
#include "stdafx.h"
#include <iostream>
See this post for more information.
First of all:
#include <iostream>
instead of #include "iostream"
Secondly, it is generally considered bad practice to write using namespace std;, even though most courses start with that. It is better to only use what you actually need, in your case:
using std::cout;
#include "iostream"
should be
#include <iostream>
Quoting from this post:difference-between-iostream-and-iostream-quotes-in-include
By courtesy of #Jerry Coffin's answer:
When you use < >, the compiler only looks in the system-designated directory/directories (e.g., whatever you've set in the include environment variable) for the header.
When you use " ", the compiler looks in the local directory first, and if that fails, re-searches just like you'd used < >. Technically, (i.e., according to the standard) that doesn't have to be the "local" directory, but that's how it works in essentially every compiler of which I'm aware).
EDIT:
However, the root cause is that stdafx.h is a precompiled header. Visual C++ will not compile anything before the #include "stdafx.h" in the source file, unless the compile option /Yu'stdafx.h' is unchecked (by default); it assumes all code in the source up to and including that line is already compiled. However, it is still better to use <> with iostream not to confuse reader of the code.
If you use #include <iostream> with the <> instead of "" then it should work. Right now, the compiler doesn't know where to find the iostream library.
Also, you might want to change cout<<"example"; to cout<<"example"<<endl; for a new line so that it formats correctly.
Came across this issue while trying to build a Dynamic Linked Library. Make sure that instead of the #include stdafx.h you specify the following include on the first line of your .cpp file:
#include "pch.h"
This should also be the case for VS2017 or earlier.
This error also occurred in the Visual Studio 2017 IDE. Moving stdafx.h to the top solved the error.
For more on stdafx.h, see What's the use for "stdafx.h" in Visual Studio?

Syntax errors in C++ include file

I'm writing a game in c++ in microsoft visual studio 2010, yesterday I wrote a pong game and everything was fine but now the compiler telling me that there is a lot of errors for example:
1>w:\c++\planet escape\planet escape\room.h(25): error C2061: syntax error : identifier 'WorldMap'
And here is the Room.h file:
#pragma once
#include <allegro5/allegro.h>
#include <vector>
#include "Entity.h"
#include "WorldMap.h"
#include "Link.h"
#define ROOM_W 20
#define ROOM_H 20
class Room{
private:...
public:...
};
When in code there is no mistakes and it sees all the classes fine.
So what can cause such mistake?
EDIT:
here is the WorldMap.h
#pragma once
#include <allegro5/allegro.h>
#include "Room.h"
#include "Player.h"
#define WORLD_W 10
#define WORLD_H 10
class WorldMap{
private:...
public:...
};
If when I'm runing it he cant see it then why he see it when coding?
You have circular includes. Suppose you are compiling a file that has a #include "WorldMap.h" as the first applicable #include statement. The file WorldMap.h has that #include "Room.h" that is going to cause a lot of trouble. The problems start in earnest in Room.h at the #include "WorldMap.h" statement. That #include has no effect thanks to the #pragma once in WorldMap.h. When the compiler gets to the point of processing the main body of Room.h, the class WorldMap is neither defined nor declared.
Addendum
The solution is to get rid of those extraneous #include statements. The file WorldMap.h does not need to #include either of Room.h or Player.h. It instead needs to make forward declarations of the classes Room and Player. Similarly, you don't need all those #include statements in Room.h either.
It is in general a good idea to use forward declarations of types in your headers instead of including the file that defines the types. If the code in the header does not need to know details of the type in question, just use a forward declaration. Do not #include the header that defines the type.