help with headder file containing class definition :)

Started by
19 comments, last by Rhaal 19 years ago
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.
These tears..leave scars...as they run down my face.
Advertisement
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.
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
(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
-Truth is out there-
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.
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
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 =)
-Truth is out there-
Quote:Original post by Athos
heh you beat me before i could edit =)


you can call me "Speed Swede" ;)
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
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);}              
These tears..leave scars...as they run down my face.
yep, you need inclusion guards alright, add those and see what happens :)
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
What about 'int draw(HWND);' being declared twice in the header file? ;)

.Amar0k."In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." -- Douglas Adams
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_HEADDERclass 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;}
These tears..leave scars...as they run down my face.

This topic is closed to new replies.

Advertisement