Sign in to follow this  
donjonson

help with headder file containing class definition :)

Recommended Posts

donjonson    271
hello, I am trying to make a header file with a class definition. I declare the member functions from within the class definition. Should I define the member functions from within that same header file or should I make a second cpp file and include the headder file? right now i am using the 2nd configuration and I cant seem to get it to compile.

Share this post


Link to post
Share on other sites
what error do you get? sounds like you've forgot to put inclusion guards


#ifndef MY_HEADER_INCLUDED
#define MY_HEADER_INCLUDED
//.. teh funkay schtuff!
#endif//MY_HEADER_INCLUDED


in your header :) that usually gives that sort of problems.

Share this post


Link to post
Share on other sites
Athos    190
(header.h)
#pragma once


class myClass
{
public:
myClass();
~myClass();

private:
int classmember;
};

(end of header file )

(header.cpp)

#include header.h

myClass::myClass()
{
classmember = 6;
}

myClass::~myClass()
{

}

(end of cpp file )



the (end of file) and (header.h) means the diffrent documents.
This should work.

Greetings



*edit*
If you are using visual studios you can use pragma once. If not you should use #ifndef #define and #endif. Personally i always youse ifndef/define/endif

Share this post


Link to post
Share on other sites
Athos    190
Quote:
Original post by DigitalDelusion
note that "#pragma once" is very compiler centric... but if you don't care about portability it's a very slick option and actually seems to reduce buildtimes.


heh you beat me before i could edit =)

Share this post


Link to post
Share on other sites
donjonson    271
Hmm I did not use any of those tags.

the error I am getting is as follows

the class has a member function called draw, when I try to defin that function in the headder.cpp and compile It tells me that member function was not declared in the class. But it clearly is defined.

lines.h


class Line;

{

public:
double X,Y,Len,Theta;
int T = 1;
Line(HWND, double, double, double, double, int, bool);
Line(HWND, double, double, double, double, int);
Line();
int draw(HWND);
void Create(HWND, double, double, double, double, int, bool);
int draw(HWND);


private:
HWND Hwnd;
};




lines.cpp

#include "lines.h"
#include <math.h>
#include <windows.h>




int Line::draw(HWND Hwnd)
{
int hT = (T / 2);
int virt = false;
int horiz = false;
int diag = false;

int start[] = {(int) X, (int) Y};
int end[] = {(int) (cos(Theta) * Len), (int) (sin(Theta) * Len)};
//check to see if line is horizontle, virticle or diagonal.

virt = (start[1] == end[1]) ? 1 : 0;
horiz = (start[0] == end[0]) ? 2 : 0;
diag = (start[1] == end[1]) && (start[0] == end[0]) ? 3 : 0;
int lino = horiz + virt + diag;

switch (lino)
{
case 1:
{
//draw line horiz
HDC hDC;
hDC = GetDC(Line::Hwnd);
MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);


if ( T > 4)
{
for(int c = 0; c < hT; c++)
{
start[1]++;
end[1]++;

MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);
}

start[0] = (int) X;
start[1] = (int) Y;
end[0] = (int) (cos(Theta) * Len);
end[1] = (int) (sin(Theta) * Len);


for(int c = 0; c < hT; c++)
{
start[1]--;
end[1]--;

MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);
}
}
ReleaseDC(Hwnd,hDC);
break;
}
case 2:
{
//draw line virt
HDC hDC;
hDC = GetDC(Line::Hwnd);
MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);

if ( T > 4)
{
for(int c = 0; c < hT; c++)
{
start[0]++;
end[0]++;

MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);
}

start[0] = (int) X;
start[1] = (int) Y;
end[0] = (int) (cos(Theta) * Len);
end[1] = (int) (sin(Theta) * Len);

for(int c = 0; c < hT; c++)
{
start[0]--;
end[0]--;

MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);
}
}
ReleaseDC(Hwnd,hDC);
break;
}
case 3:
{
//draw line diag
HDC hDC;
hDC = GetDC(Line::Hwnd);
MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);

if ( T > 4)
{
int Signx;
int Signy;
if(start[0] < end[0])
Signx = 1;
if(start[1] < end[1])
Signy = 1;
if(start[0] > end[0])
Signy = 0;
if(start[1] > end[1])
Signy = 0;


if (Signx == Signy)
{
for(int c = 0; c < hT; c++)
{
start[0]++;
end[0]++;
start[1]++;
end[1]++;

MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);
}

start[0] = (int) X;
start[1] = (int) Y;
end[0] = (int) (cos(Theta) * Len);
end[1] = (int) (sin(Theta) * Len);

for(int c = 0; c < hT; c++)
{
start[0]--;
end[0]--;
start[1]--;
end[1]--;

MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);
}
}
if(Signx != Signy)
{
for(int c = 0; c < hT; c++)
{
start[0]++;
end[0]++;
start[1]--;
end[1]--;

MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);
}

start[0] = (int) X;
start[1] = (int) Y;
end[0] = (int) (cos(Theta) * Len);
end[1] = (int) (sin(Theta) * Len);

for(int c = 0; c < hT; c++)
{
start[0]--;
end[0]--;
start[1]++;
end[1]++;

MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);
}
}

}
ReleaseDC(Hwnd,hDC);
break;
}
}

return 0;
}



Line::Line(HWND lHwnd, double lx, double ly, double lLen, double lTheta, int lT, bool drw)
{
X = lx;
Y = ly;
Len = lLen;
Theta = lTheta;
T = lT;
Hwnd = lHwnd;


if(drw)
draw(Hwnd);
}

Line::Line(HWND lHwnd, double lx, double ly, double lLen, double lTheta, int lT)
{
X = lx;
Y = ly;
Len = lLen;
Theta = lTheta;
T = lT;
Hwnd = lHwnd;
}

Line::Line()
{
}


void Line::Create(HWND lHwnd, double lx, double ly, double lLen, double lTheta, int lT, bool drw)
{
X = lx;
Y = ly;
Len = lLen;
Theta = lTheta;
T = lT;
Hwnd = lHwnd;


if(drw)
draw(Hwnd);
}





Share this post


Link to post
Share on other sites
donjonson    271
added the inclusion guard and fixed the double declaration of draw. I am still getting the same compil error.

The compiler stops at the first use of draw() in the lines.cpp file.

the exact error is as follows

"In constructor `Line::Line(HWND__*, double, double, double, double, int, bool)':
`draw' undeclared (first use this function) "

Now that is just not true because it is declared in the class definition.

am I doing something wrong?

here is the current code.

lines.h

#include <math.h>
#ifndef _LINES_HEADDER
#define _LINES_HEADDER
class Line;

{

public:
double X,Y,Len,Theta;
int T = 1;
Line(HWND, double, double, double, double, int, bool);
Line(HWND, double, double, double, double, int);
Line();
int draw(HWND);
void Create(HWND, double, double, double, double, int, bool);


private:
HWND Hwnd;
};


#endif





lines.cpp

#include "lines.h"
#include <math.h>
#include <windows.h>







Line::Line(HWND lHwnd, double lx, double ly, double lLen, double lTheta, int lT, bool drw)
{
X = lx;
Y = ly;
Len = lLen;
Theta = lTheta;
T = lT;
Hwnd = lHwnd;


if(drw)
draw(Hwnd);
}

Line::Line(HWND lHwnd, double lx, double ly, double lLen, double lTheta, int lT)
{
X = lx;
Y = ly;
Len = lLen;
Theta = lTheta;
T = lT;
Hwnd = lHwnd;
}

Line::Line()
{
}


void Line::Create(HWND lHwnd, double lx, double ly, double lLen, double lTheta, int lT, bool drw)
{
X = lx;
Y = ly;
Len = lLen;
Theta = lTheta;
T = lT;
Hwnd = lHwnd;


if(drw)
draw(Hwnd);
}


int Line::draw(HWND Hwnd)
{
int hT = (T / 2);
int virt = false;
int horiz = false;
int diag = false;

int start[] = {(int) X, (int) Y};
int end[] = {(int) (cos(Theta) * Len), (int) (sin(Theta) * Len)};
//check to see if line is horizontle, virticle or diagonal.

virt = (start[1] == end[1]) ? 1 : 0;
horiz = (start[0] == end[0]) ? 2 : 0;
if((virt + horiz) == 0)
diag = 3;
else
diag = 0;

int lino = horiz + virt + diag;

switch (lino)
{
case 1:
{
//draw line horiz
HDC hDC;
hDC = GetDC(Line::Hwnd);
MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);


if ( T > 4)
{
for(int c = 0; c < hT; c++)
{
start[1]++;
end[1]++;

MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);
}

start[0] = (int) X;
start[1] = (int) Y;
end[0] = (int) (cos(Theta) * Len);
end[1] = (int) (sin(Theta) * Len);


for(int c = 0; c < hT; c++)
{
start[1]--;
end[1]--;

MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);
}
}
ReleaseDC(Hwnd,hDC);
break;
}
case 2:
{
//draw line virt
HDC hDC;
hDC = GetDC(Line::Hwnd);
MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);

if ( T > 4)
{
for(int c = 0; c < hT; c++)
{
start[0]++;
end[0]++;

MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);
}

start[0] = (int) X;
start[1] = (int) Y;
end[0] = (int) (cos(Theta) * Len);
end[1] = (int) (sin(Theta) * Len);

for(int c = 0; c < hT; c++)
{
start[0]--;
end[0]--;

MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);
}
}
ReleaseDC(Hwnd,hDC);
break;
}
case 3:
{
//draw line diag
HDC hDC;
hDC = GetDC(Line::Hwnd);
MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);

if ( T > 4)
{
int Signx;
int Signy;
if(start[0] < end[0])
Signx = 1;
if(start[1] < end[1])
Signy = 1;
if(start[0] > end[0])
Signy = 0;
if(start[1] > end[1])
Signy = 0;


if (Signx == Signy)
{
for(int c = 0; c < hT; c++)
{
start[0]++;
end[0]++;
start[1]++;
end[1]++;

MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);
}

start[0] = (int) X;
start[1] = (int) Y;
end[0] = (int) (cos(Theta) * Len);
end[1] = (int) (sin(Theta) * Len);

for(int c = 0; c < hT; c++)
{
start[0]--;
end[0]--;
start[1]--;
end[1]--;

MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);
}
}
if(Signx != Signy)
{
for(int c = 0; c < hT; c++)
{
start[0]++;
end[0]++;
start[1]--;
end[1]--;

MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);
}

start[0] = (int) X;
start[1] = (int) Y;
end[0] = (int) (cos(Theta) * Len);
end[1] = (int) (sin(Theta) * Len);

for(int c = 0; c < hT; c++)
{
start[0]--;
end[0]--;
start[1]++;
end[1]++;

MoveToEx(hDC, end[0], end[1], NULL);
LineTo(hDC, start[0], start[1]);
}
}

}
ReleaseDC(Hwnd,hDC);
break;
}
}

return 0;
}



Share this post


Link to post
Share on other sites
Athos    190
"class Line;"

I dont think it likes your ";" at the end of class decleration.
And i think you have forgotten the destructor for the class too.

Greetings

Share this post


Link to post
Share on other sites
donjonson    271
That is wierd because I have a c++ book that told me I have to put ; at the end of every class declaration. I will try taking it off. the book also told me that destructors are optional. I will add one and see.

EDIT: no that didn't fix the problem
:(

Share this post


Link to post
Share on other sites
Quote:
Original post by donjonson
That is wierd because I have a c++ book that told me I have to put ; at the end of every class declaration. I will try taking it off.

EDIT: no that didn't fix the problem
:(


well you wan't one at the end after the closing }

class A; //<--- no semicolon here please...
{
};//<-- but this one must be present

Share this post


Link to post
Share on other sites
Athos    190
you have included <window.h> in the cpp file but you still use the HWND in the header file.

So one thing is to move <window.h> to the .h file.

I think its the last thing i can see.

Greetings

Share this post


Link to post
Share on other sites
donjonson    271
Thanks for all your help But I still cant get it to compile.
If I move the definition of the draw function to the top of the lines.cpp file. it flags that saying that there is no member function called Line::draw(HWND), but it wont flag on the other member function definitions in lines.cpp. so for some reason it is excluding draw in the class definition.

Share this post


Link to post
Share on other sites
donjonson    271
ok I figured out the problem.

for some reason devC++ was not saving my edits to the files. If I open up the project and make an edit and save the change will be there next time I open the project. but if i close the project and open the file individualy, the change will not have been made. hmm strange bug. In the actual file the draw declaration was commented out. I fixed it and it now compiles.

Share this post


Link to post
Share on other sites
Oberon_Command    6087
You don't actually need to include windows.h in your class declaration. You just need to include the class header file AFTER windows.h. That way, the preprocessor treats it like its been declared after the stuff in windows.h.

Share this post


Link to post
Share on other sites
Quote:
Original post by Oberon_Command
You don't actually need to include windows.h in your class declaration. You just need to include the class header file AFTER windows.h. That way, the preprocessor treats it like its been declared after the stuff in windows.h.


And that's wrong for so many reason that I can't count them.
always, ALWAYS! make headers standalone if you can't pull in one header without also having to figure out what other arcane headers are needed you'll end up pulling your hair out later when you try to reuse stuff.

That doesn't imply including everything and it's mother forward declarations can many times suffice for headers but really that's one of the worst pieces of advice I heard on theese boards to date.

Share this post


Link to post
Share on other sites
Rhaal    754
Indeed. Most companies will even have a standard to include header files directly. Never assume that your header file is going to include all the other necessary header files needed by your implementation.

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