Sign in to follow this  
Shamino

Can't find the error in this

Recommended Posts

Okay, here is the code: Renderer.h
#include <vector>
#include "Vector3.h"

using namespace std;

struct	BasicTri
{
	Vector3 P1, P2, P3;
	int	Texture;


	BasicTri(const BasicTri& t)
	{
		P1 = t.P1;
		P2 = t.P2;
		P3 = t.P3;
		Texture = t.Texture;
	}

	BasicTri& operator=(const BasicTri& t)
	{
		if( &t != this ){
			P1 = t.P1;
			P2 = t.P2;
			P3 = t.P3;
			Texture = t.Texture;
		}
		return *this;
	}
};

std::vector<BasicTri*>	mBasicTri;

class GLRenderer
{
public:

	void	AddBasicTriToRenderer(BasicTri* t)
	{

		// then add to the vector
		mBasicTri.push_back(t);
	}

	void	RenderBasicTriangles(void);

};
Renderer.cpp
#include "Renderer.h"
Heh, yeah, and here is the error...
--------------------Configuration: NeHeGL - Win32 Debug--------------------
Compiling...
Renderer.cpp
Linking...
Renderer.obj : error LNK2005: "class std::vector<struct BasicTri *,class std::allocator<struct BasicTri *> > mBasicTri" (?mBasicTri@@3V?$vector@PAUBasicTri@@V?$allocator@PAUBasicTri@@@std@@@std@@A) already defined in Example.obj
Debug/NeHeGL.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.

NeHeGL.exe - 2 error(s), 0 warning(s)
any help on this one? I can't find any multiply defined symbols?

Share this post


Link to post
Share on other sites
You can't define global variables like that in headers. In the header declare the variable as extern and then in one source file (probably renderer.cpp) define it. ex:
renderer.h
extern std::vector<BasicTri*> mBasicTri;

renderer.cpp
std::vector<BasicTri*> mBasicTri;

Share this post


Link to post
Share on other sites
Would you mind helping me actually write the uhm, RenderBasicTriangles() function?

I know I need a for loop and go from the beginning of the vector to the end, but what goes in the loopage?

Share this post


Link to post
Share on other sites
Also, it's a very bad idea to do the whole, "using namespace std" in a header file. None of your supplied code needs it. If your functions need it in the .cpp file add it in there only.

Share this post


Link to post
Share on other sites
Really? Don't the vector functions in my AddBasicTriToRenderer() function need that? And if so, I just move that to the CPP file?


:: Hmm, guess not, edited it out, runs fine, exactly what is that used for?

Share this post


Link to post
Share on other sites
Quote:
Original post by Shamino
Really? Don't the vector functions in my AddBasicTriToRenderer() function need that? And if so, I just move that to the CPP file?


:: Hmm, guess not, edited it out, runs fine, exactly what is that used for?


OpenGL without knowing what the std:: namespace is for? No offence.

All the STL functions in C++ are under the namespace std. If you use.. say 'string', you need string.h and you need the std namespace, because 'string' is in there and only in there. So, 'std::string'. Or, using namespace std; for all needs.

A namespace:

namespace foo {
int foobar(char bar) { return bar; }
}


Simple.

Share this post


Link to post
Share on other sites
If you remove using namespace std; then you will have to prefix everything in that namespace with an std:: (ie, vectors, strings, etc). He's right that you probably don't want it in the header because then EVERYTHING (that includes that header) will now be using that namespace. In the cpp is good because then it's only at a single file scope as needed.

With that said, I throw it in my precompiled header that I include in every single file in my game because I'm a lazy bastard (typing std:: in headers annoys me due to the clutter) and I haven't personally ran into a problem with it yet. I realize it's not ideal but hell with it.

Share this post


Link to post
Share on other sites
You can also just use the parts of the namespace you need:
#include <iostream>

using std::cout;
using std::endl;

int foo()
{
cout << "hello world" << endl;
}

Share this post


Link to post
Share on other sites
Here it is, my tri renderer!


Renderer.h

#include <vector>
#include "Vector3.h"

struct BasicTri
{
Vector3 P1;
Vector3 P2;
Vector3 P3;
int Texture;

BasicTri() { }

BasicTri(const BasicTri& t)
{
P1 = t.P1;
P2 = t.P2;
P3 = t.P3;
Texture = t.Texture;
}

BasicTri& operator=(const BasicTri& t)
{
if( &t != this ){
P1 = t.P1;
P2 = t.P2;
P3 = t.P3;
Texture = t.Texture;
}
return *this;
}
};

extern std::vector<BasicTri*> mBasicTri;


class GLRenderer
{
public:

void AddBasicTriToRenderer(BasicTri* t)
{

// then add to the vector
mBasicTri.push_back(t);
}

void RenderBasicTriangles(void);

};


Renderer.cpp

#include <windows.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glaux.h>
#include "NeHeGL.h"
#include "Renderer.h"

std::vector<BasicTri*> mBasicTri;

void GLRenderer::RenderBasicTriangles()
{
if (!mBasicTri.size())
{
return;
}

std::vector<BasicTri*>::iterator ptr;

for (ptr = mBasicTri.begin(); ptr != mBasicTri.end(); ptr++)
{
BasicTri * temp = *ptr;

glBegin(GL_TRIANGLES);
glVertex3f(temp->P1.x,temp->P1.y,temp->P1.z);
glVertex3f(temp->P2.x,temp->P2.y,temp->P2.z);
glVertex3f(temp->P3.x,temp->P3.y,temp->P3.z);
glEnd();
}

}


Vector3.h

class Vector3
{
public:
float x, y, z;

Vector3()
{
}

Vector3(float x, float y, float z);

// And so on
};


Vector3.cpp

#include "Vector3.h"


Vector3::Vector3(float X, float Y, float Z)
{
x = X;
y = Y;
z = Z;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this