Help with my project, Visual C++

thIS IS MY CODE to do some project of solar system. i wanted to create dynamic planets, and store their color for picking. i have included only 2/4 files. the thing is it compiles properly, but this error is genrated. in other.h all the decalrations is there. in another file, all the functions are declared.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//other.h
#include <ctime>
#include <cstdlib>
#include <iostream>
#include "common.h"

#include <math.h>
#include <conio.h>
#include <string.h>
#ifdef WIN32
#include <windows.h>
#endif
#include <GL/glut.h>
#include <stdio.h>
#define INIT_VIEW_X    0.0 
#define INIT_VIEW_Y    0.0 
#define INIT_VIEW_Z   -4.5 
#define VIEW_LEFT     -2.0 
#define VIEW_RIGHT     2.0 
#define VIEW_BOTTOM   -2.0 
#define VIEW_TOP       2.0 
#define VIEW_NEAR      1.0 
#define VIEW_FAR     200.0 
float camrot_ang = 90.0;
float camrot_x = 1.0;
float camrot_y = 0.0;
float camrot_z = 0.0;
float sunrev = 0.0;        // the amount of revolution of the sun
float delsunrev = 0.0; // the amount of change in the revolution (rotation) per frame of animation
float delplanetrev = 0.0;
float delang = 0.1;   // the amount of change in the rate of rotation per key press
float planetrev =0.0;
float planetrot =0.0;
int fullscreen = 0;               
GLfloat zdist =-4.5;
GLfloat Ratio; 
GLfloat AmbientLight[]  = {0.9f, 0.4f, 0.1f, 1.0f}; 
GLfloat DiffuseLight[]  = {0.9f, 0.9f, 0.5f, 1.0f}; 
GLfloat SpecularLight[] = {1.0f, 1.0f, 1.0f, 1.0f}; 
GLfloat SpecRef[]       = {0.7f, 0.7f, 0.7f, 1.0f}; 
GLfloat LightPos[]      = {-50.0f,50.0f,100.0f,30.0f}; 
GLubyte Shine           = 1;
float mx[10]={0},my[10] ={0};
float zoom=1;
float orbitDegrees = 0;
int mkey,mstate;
float planetcount=0;

int *R, *G, *B;
int totcol=1,n;
void MyFunc();
void SetupRend();
void Display(void);
void ChangeWindow(GLsizei w, GLsizei h);
void CheckRot(float *x);
void SpecialKeys(int key, int x, int y);
void KeyPress (unsigned char key, int x, int y);
void animate(void);
void mymotionmouse(int x,int y);
void mymouse(int key, int state, int x, int y);
void createplanet(int n, int *red, int *grn, int *blu);


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//mycode.cpp this is the main thing

#include "other.h"

void Display(void) 
{	
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
	createplanet(n, R, G, B);
	glMatrixMode(GL_PROJECTION); 
	glLoadIdentity(); 
	gluPerspective(50.0*zoom, 1.3, 3.0, 7.0); 
	glMatrixMode(GL_MODELVIEW); 
	glLoadIdentity(); 
	gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
	glRotatef(orbitDegrees, 1, 1, 0);
    glutSwapBuffers(); 
}

void main()
{
	printf("Enter the number\n");
	scanf("%d", &n);
	R = new int[totcol];
	G = new int[totcol];
	B = new int[totcol];
	printf("Gen colors OK\n");
	SetupRend();
	glutDisplayFunc(Display);
	glutReshapeFunc(ChangeWindow);
	glutKeyboardFunc (KeyPress);                            // function to handle key press 
    glutSpecialFunc (SpecialKeys); 
	glutIdleFunc(animate);  // updates object motions 
	glutMouseFunc(mymouse);
	glutMotionFunc(mymotionmouse);
	glutMainLoop();
}

The createplanet function is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void createplanet(int n, int *red, int *grn, int *blu)
{
	float plntrot, plntrev;
	float radius=0;
	while(n<10) {
		srand((unsigned)time(NULL));
		radius = int(rand()*1000/999);
		radius*=0.011;
		printf("Radius of %d is %f", n, radius);
		srand((unsigned)time(NULL));
		red[totcol] = int(rand()*255/256);
		grn[totcol] = int(rand()*255/256);
		blu[totcol] = int(rand()*255/256);
		totcol++;
		plntrot = int(rand()*359/360);
		plntrev = int(rand()*359/360);
		planetcount+=0.2;
		glPushMatrix();
			glRotatef(camrot_ang,camrot_x,camrot_y,camrot_z);
			glRotatef(plntrev,0.0, 0.0, 1.0);
			glTranslatef(planetcount+=0.2,0.0,0.0);
			glRotatef(plntrot,0.0,0.0,1.0);
			glColor3ub(red[totcol],grn[totcol],blu[totcol]);
			glutSolidSphere(radius,20,20);
		glPopMatrix();
		n++;
	}
}

this is the output
1>------ Build started: Project: planet_project, Configuration: Debug Win32 ------
1>Compiling...
1>mycode.cpp
1>c:\users\banehallow\documents\visual studio 2008\projects\planet_project\planet_project\mycode.cpp(20) : warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\stdio.h(306) : see declaration of 'scanf'
1>text.cpp
1>Generating Code...
1>Linking...
1>text.obj : error LNK2005: "int mkey" (?mkey@@3HA) already defined in mycode.obj
1>text.obj : error LNK2005: "int * G" (?G@@3PAHA) already defined in mycode.obj
1>text.obj : error LNK2005: "int * R" (?R@@3PAHA) already defined in mycode.obj
1>text.obj : error LNK2005: "float Ratio" (?Ratio@@3MA) already defined in mycode.obj
1>text.obj : error LNK2005: "int mstate" (?mstate@@3HA) already defined in mycode.obj
1>text.obj : error LNK2005: "int n" (?n@@3HA) already defined in mycode.obj
1>text.obj : error LNK2005: "int * B" (?B@@3PAHA) already defined in mycode.obj
1>text.obj : error LNK2005: "float camrot_ang" (?camrot_ang@@3MA) already defined in mycode.obj
1>text.obj : error LNK2005: "float camrot_x" (?camrot_x@@3MA) already defined in mycode.obj
1>text.obj : error LNK2005: "float camrot_y" (?camrot_y@@3MA) already defined in mycode.obj
1>text.obj : error LNK2005: "float camrot_z" (?camrot_z@@3MA) already defined in mycode.obj
1>text.obj : error LNK2005: "float sunrev" (?sunrev@@3MA) already defined in mycode.obj
1>text.obj : error LNK2005: "float delsunrev" (?delsunrev@@3MA) already defined in mycode.obj
1>text.obj : error LNK2005: "float delplanetrev" (?delplanetrev@@3MA) already defined in mycode.obj
1>text.obj : error LNK2005: "float delang" (?delang@@3MA) already defined in mycode.obj
1>text.obj : error LNK2005: "float planetrev" (?planetrev@@3MA) already defined in mycode.obj
1>text.obj : error LNK2005: "float planetrot" (?planetrot@@3MA) already defined in mycode.obj
1>text.obj : error LNK2005: "int fullscreen" (?fullscreen@@3HA) already defined in mycode.obj
1>text.obj : error LNK2005: "float zdist" (?zdist@@3MA) already defined in mycode.obj
1>text.obj : error LNK2005: "float * AmbientLight" (?AmbientLight@@3PAMA) already defined in mycode.obj
1>text.obj : error LNK2005: "float * DiffuseLight" (?DiffuseLight@@3PAMA) already defined in mycode.obj
1>text.obj : error LNK2005: "float * SpecularLight" (?SpecularLight@@3PAMA) already defined in mycode.obj
1>text.obj : error LNK2005: "float * SpecRef" (?SpecRef@@3PAMA) already defined in mycode.obj
1>text.obj : error LNK2005: "float * LightPos" (?LightPos@@3PAMA) already defined in mycode.obj
1>text.obj : error LNK2005: "unsigned char Shine" (?Shine@@3EA) already defined in mycode.obj
1>text.obj : error LNK2005: "float * mx" (?mx@@3PAMA) already defined in mycode.obj
1>text.obj : error LNK2005: "float * my" (?my@@3PAMA) already defined in mycode.obj
1>text.obj : error LNK2005: "float zoom" (?zoom@@3MA) already defined in mycode.obj
1>text.obj : error LNK2005: "float orbitDegrees" (?orbitDegrees@@3MA) already defined in mycode.obj
1>text.obj : error LNK2005: "float planetcount" (?planetcount@@3MA) already defined in mycode.obj
1>text.obj : error LNK2005: "int totcol" (?totcol@@3HA) already defined in mycode.obj
1>C:\Users\Banehallow\Documents\Visual Studio 2008\Projects\planet_project\Debug\planet_project.exe : fatal error LNK1169: one or more multiply defined symbols found
1>Build log was saved at "file://c:\Users\Banehallow\Documents\Visual Studio 2008\Projects\planet_project\planet_project\Debug\BuildLog.htm"
1>planet_project - 32 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


what is this error? what should i change?
the thing is it compiles properly, but this error is genrated.


If you're getting errors... it is not compiling properly =P


Anyway the problem is because you are using global variables. Rather than list the many reasons why... I just say that they're "bad".

Specifically.. the error you're getting is because variables are getting defined in each source file that you #include that header in. I get into details in this thread:

http://www.cplusplus.com/forum/general/71964/#msg383802
THanks for the reply, its linker error right?.. and its very hard not to use global variables. well your telling me that i should pass by reference to all the functions and use that?? all pointers and all? thats very hard man. Im the sole coder of my team of two, and im trying to do something complex, while others just copied the code from PROJECT websites!

what does
error LNK2005: "int totcol" (?totcol@@3HA) already defined in mycode.obj

mean?

is there any kind of work around, coz i have written so much of the code. i really cannot suffer making it all pass by references. does linker error hav any workaround?
i got it to work
used extern in other.h and removed all initializations in that header. now it executes, but this is the error later..
'planet_project.exe': Loaded 'C:\Users\Banehallow\Documents\Visual Studio 2008\Projects\planet_project\Debug\planet_project.exe', Symbols loaded.
'planet_project.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll'
'planet_project.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll'
'planet_project.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll'
'planet_project.exe': Loaded 'C:\Windows\SysWOW64\opengl32.dll'
'planet_project.exe': Loaded 'C:\Windows\SysWOW64\msvcrt.dll'
'planet_project.exe': Loaded 'C:\Windows\SysWOW64\advapi32.dll'
'planet_project.exe': Loaded 'C:\Windows\SysWOW64\sechost.dll'
'planet_project.exe': Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'
'planet_project.exe': Loaded 'C:\Windows\SysWOW64\sspicli.dll'
'planet_project.exe': Loaded 'C:\Windows\SysWOW64\cryptbase.dll'
'planet_project.exe': Loaded 'C:\Windows\SysWOW64\gdi32.dll'
'planet_project.exe': Loaded 'C:\Windows\SysWOW64\user32.dll'
'planet_project.exe': Loaded 'C:\Windows\SysWOW64\lpk.dll'
'planet_project.exe': Loaded 'C:\Windows\SysWOW64\usp10.dll'
'planet_project.exe': Loaded 'C:\Windows\SysWOW64\glu32.dll'
'planet_project.exe': Loaded 'C:\Windows\SysWOW64\ddraw.dll'
'planet_project.exe': Loaded 'C:\Windows\SysWOW64\dciman32.dll'
'planet_project.exe': Loaded 'C:\Windows\SysWOW64\setupapi.dll'
'planet_project.exe': Loaded 'C:\Windows\SysWOW64\cfgmgr32.dll'
'planet_project.exe': Loaded 'C:\Windows\SysWOW64\oleaut32.dll'
'planet_project.exe': Loaded 'C:\Windows\SysWOW64\ole32.dll'
'planet_project.exe': Loaded 'C:\Windows\SysWOW64\devobj.dll'
'planet_project.exe': Loaded 'C:\Windows\SysWOW64\dwmapi.dll'
'planet_project.exe': Loaded 'C:\Windows\SysWOW64\glut32.dll', Binary was not built with debug information.
'planet_project.exe': Loaded 'C:\Windows\SysWOW64\winmm.dll'
'planet_project.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc90.debugcrt_1fc8b3b9a1e18e3b_9.0.21022.8_none_96748342450f6aa2\msvcr90d.dll', Symbols loaded.
'planet_project.exe': Loaded 'C:\Windows\SysWOW64\imm32.dll'
'planet_project.exe': Loaded 'C:\Windows\SysWOW64\msctf.dll'
'planet_project.exe': Loaded 'C:\ProgramData\BrowserProtect\2.6.1125.80\{c16c1ccb-7046-4e5c-a2f3-533ad2fec8e8}\BrowserProtect.dll'
'planet_project.exe': Loaded 'C:\Windows\SysWOW64\shell32.dll'
'planet_project.exe': Loaded 'C:\Windows\SysWOW64\shlwapi.dll'
'planet_project.exe': Loaded 'C:\Windows\SysWOW64\version.dll'
'planet_project.exe': Loaded 'C:\Windows\SysWOW64\imagehlp.dll'
'planet_project.exe': Loaded 'C:\Windows\SysWOW64\psapi.dll'
First-chance exception at 0x1000a8f5 in planet_project.exe: 0xC0000005: Access violation writing location 0x000000a8.
Unhandled exception at 0x76fa15de in planet_project.exe: 0xC0000005: Access violation writing location 0x000000a8.


it also opens up tidtable.c, osfinfo.c, and crtexe.c and mlock.c.... why? any idea? i think its coz of memory acces violation. this is the problem with pointers..
Last edited on
Looks like you're dereferencing a null pointer. When you get the access violation... select "Retry" or whatever the option is in the popup box to snap the debugger. It will take you right to the offending line of code.

Examine variable contents (open up a "Watch" window) and make sure your indexes are in bounds and your pointers are non-null.
That worked for me "Disch"
removed the pointers and its all good now. thanks a lot !
Last edited on
Topic archived. No new replies allowed.