Windows and cpp only not working

So I recently decided to try switching my projects to use cpp files only, which I found a lot easier to work with on linux than those with generated header files. This approach worked okay with linux g++, but MSVC++ 2010 on windows is coughing up a hairball. Trying the following code in MSVC++ 2010,

https://www.dropbox.com/s/2zpn9rmydyb7rva/FuuBar.zip

I get multiple link 2005 errors indicating that the linker is not properly sorting which symbols should go in which objects.

The code is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// FuuBar
#include<iostream>
#include "Bar.cpp"


int main()
{	Fuu * fuu, * bar;
	fuu = new Fuu();
	bar = new Bar();
	fuu->FuuBar();
	bar->FuuBar();
	std::string hold;
	std::cin >> hold;
	delete fuu;
	delete bar;
	return 0;
	
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <string>
#include <iostream>

class Fuu
{	public:
	Fuu();
	virtual void FuuBar();
	~Fuu();
};

Fuu::Fuu()
{
}

void Fuu::FuuBar()
{	std::cout << "Fuu" << std::endl;
}


Fuu::~Fuu()
{
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "Fuu.cpp"

class Bar: public Fuu
{	public:
	Bar();
	void FuuBar();
	~Bar();
};

Bar::Bar()
{
}

void Bar::FuuBar()
{	std::cout << "Bar" << std::endl;
}

Bar::~Bar()
{
}

Last edited on
Why are you including *.cpp files?

This is why you're getting linker errors, most likely for multiply defined symbols. Put your class declarations into header files. Your class definitions into cpp source files and add them to your project in Visual Studio.

Don't forget to wrap the contents of your header files with inclusion guards.
But thats exactly my point. I thought C++ was supposed to work fine even without header files. Is that not part of the standard?
I think you're misunderstanding what's going on here.

I get multiple link 2005 errors indicating that the linker is not properly sorting which symbols should go in which objects.

It is you that's confusing the linker by #including your cpp files. When the preprocessor runs it simply replaces the #include ... directive with the contents of the #include'd file, resulting symbols being defined multiple times.

You are violating the One Definition Rule
http://en.wikipedia.org/wiki/One_Definition_Rule

So in short, don't #include cpp files
Topic archived. No new replies allowed.