Quick C++ question

Appledrop

Platinum Member
Aug 25, 2004
2,340
0
0
hi, ill make this quick..
MAIN.CPP

#include "myheader.h"
int id;


void main()
{
tmystruct player;
player.id=10;
player.active=true;
player.grade='a';
cout<<player.id<<" "<<player.active<<" "<<player.grade;
levelup(player.id);
getch();

}

void levelup(int id)
{
if(player.active==true)
{
player.grade='b';
cout<<player.id<<" "<<player.active<<" "<<player.grade;
}

}

MYHEADER.H

#ifndef _MYHEADER_H
#define _MYHEADER_H

#include <conio.h>
#include <iostream>
using namespace std;

void levelup(int id);

struct tmystruct
{
int id;
char grade;
bool active;
};

#endif


Compile error:
Compiling...
main.cpp
C:\Documents and Settings\Arr\Desktop\projects\demo\demo\main.cpp(18) : error C2065: 'player' : undeclared identifier
C:\Documents and Settings\Arr\Desktop\projects\demo\demo\main.cpp(18) : error C2228: left of '.active' must have class/struct/union type
C:\Documents and Settings\Arr\Desktop\projects\demo\demo\main.cpp(20) : error C2228: left of '.grade' must have class/struct/union type
C:\Documents and Settings\Arr\Desktop\projects\demo\demo\main.cpp(21) : error C2228: left of '.id' must have class/struct/union type
C:\Documents and Settings\Arr\Desktop\projects\demo\demo\main.cpp(21) : error C2228: left of '.active' must have class/struct/union type
C:\Documents and Settings\Arr\Desktop\projects\demo\demo\main.cpp(21) : error C2228: left of '.grade' must have class/struct/union type
Error executing cl.exe.

demo.exe - 6 error(s), 0 warning(s)


If someone could have a look and tell me what i have done wrong, would be grateful , as i have fiddled with it for quite a while now without luck!! Thanks.
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
hi

if(player.active==true))

extra ) at end

also you don't need ; at end of function
 

Umberger

Golden Member
Apr 13, 2005
1,710
0
76
Originally posted by: dighn
hi

if(player.active==true))

extra ) at end

also you don't need ; at end of function

but the extra ; isn't what's causing the problem. it's the double parenthesis. and that's not a n00b question. a n00b question is when a kid says "i don't know what this error means, and i look at the screen and it says 'error, missing semicolon on line 18' and line 18 says:
cout << "hello" << endl


(gotta love teaching entry level computer science in the summer) ;)
 

Appledrop

Platinum Member
Aug 25, 2004
2,340
0
0
mmm, thanks for that, i thought functions had to have a ;.. Anyway. I corrected both sections of code, but i still have the error
Compiling...
main.cpp
C:\Documents and Settings\Arr\Desktop\projects\demo\demo\main.cpp(18) : error C2448: '<Unknown>' : function-style initializer appears to be a function definition
Error executing cl.exe.

demo.exe - 1 error(s), 0 warning(s)

:( It isnt a major project or anything, but i like to be confident i can program correctly before i move on to more advanced stuff hehe. Anyone else?? Ta
 

Appledrop

Platinum Member
Aug 25, 2004
2,340
0
0
Originally posted by: AllGoodNamesAreTaken

Your function definition must match the prototype/signature declared in your header file.

void levelup(int id)
{
...
};


aha! At first i thought you were wrong, as it generated 6 errors when i ammended the code to that, but now i see that my original error has gone.

The new errors :
Compiling...
main.cpp
C:\Documents and Settings\Arr\Desktop\projects\demo\demo\main.cpp(18) : error C2065: 'player' : undeclared identifier
C:\Documents and Settings\Arr\Desktop\projects\demo\demo\main.cpp(18) : error C2228: left of '.active' must have class/struct/union type
C:\Documents and Settings\Arr\Desktop\projects\demo\demo\main.cpp(20) : error C2228: left of '.grade' must have class/struct/union type
C:\Documents and Settings\Arr\Desktop\projects\demo\demo\main.cpp(21) : error C2228: left of '.id' must have class/struct/union type
C:\Documents and Settings\Arr\Desktop\projects\demo\demo\main.cpp(21) : error C2228: left of '.active' must have class/struct/union type
C:\Documents and Settings\Arr\Desktop\projects\demo\demo\main.cpp(21) : error C2228: left of '.grade' must have class/struct/union type
Error executing cl.exe.

This puzzles me.. Because i read that structs are public classes, and my function doesnt seem to be able to access them ? Hehe thanks so far!
 
Apr 30, 2005
81
0
66
C:\Documents and Settings\Arr\Desktop\projects\demo\demo\main.cpp(18) : error C2065: 'player' : undeclared identifier

player is not declared in the scope of levelup(). It is declared in main(). You will need to change levelup() to take a tmystruct parameter ( either a reference or pointer, since it is poor style to pass a struct by value )

void levelup( tmystruct &rPlayer );

main()
{
tmystruct player;
...
levelup( player );
...
}

void levelup( tmystruct &rPlayer )
{
if( rPlayer.active ...
}


 

Appledrop

Platinum Member
Aug 25, 2004
2,340
0
0
ahh right thanks, i will fiddle around with pointers see if i can get it to work then =)