So this is just the beginning of my questions, but I'm seriously having problems using classes in C++, I'm writing a program right now to plot points using asterisks on a 40 by 40 plain. right now I'm just trying to write the .h for the point class and i cant even get that to compile.

heres what I have followed by errors I'm receiving... any help in regards to classes or just help with the errors will be appreciated.

#ifndef POINT_H
#define POINT_H

#include <iostream>
#include "arrayholder.h"

class Point{

 public:
  Point(): x(0.0), y(0.0), next() {};

  void addPoint( char a[][] );

private:
double x;
double y;
Point* next;

};

#endif

and my arrayholder.h file

#ifndef ARRAYHOLDER_H
#define ARRAYHOLDER_H

#include <iostream>
using namespace std;

#include "rectangle.h"
#include "point.h"

class arrayholder{

private:
char a[40][40];
Point* listpointer;

public:

 char** initarray();
 void printRect();
 void printPoints();
};

#endif

In file included from point.h:13:
arrayholder.h:22: error: ISO C++ forbids declaration of `Point' with no type
arrayholder.h:22: error: expected `;' before '*' token

I'm getting the same two errors for rectangle.h

Recommended Answers

All 18 Replies

I rewrote what you had out and included a "lib.h" file so you do not have to keep typing out all the includes.

"point.h"

#ifndef POINT_H
#define POINT_H

class POINT
{
	//for whatever reason most people that I see post put
	//"private:" here when classes are automaticly private at the start
	double x, y;
	POINT* next;
	
	public:
	POINT(): x(0.0), y(0.0), next() {};
	
	void addPoint( char **a );
};

#endif

"arrayholder.h"

#ifndef ARRAYHOLDER_H
#define ARRAYHOLDER_H

#include "point.h" //the only thing that has to be included here

class ARRAYHOLDER
{
	char a[40][40];
	POINT* listpointer;
	
	public:
	
	char** initarray();
	void printRect();
	void printPoints();	
};

#endif

"lib.h"

#ifndef LIB_H
#define LIB_H

#include <iostream>

using namespace std;

#include "arrayholder.h"
#include "point.h"


#endif

"main.cpp"

#include "lib.h" //only have to include "lib.h" file

int main()
{
	
	system("PAUSE");
	return 0;
}

If you were to make a "point.cpp" file to hold all the functions in it you just have to include "lib.h" and then for each function type returnType POINT::functionName(parameters) This is just incase you do not know how to do that and if you do then you can just ignore it.

for some reason I'm still getting the same error for rectangle.h when I attempt to compile lib.h... here is rectangle.h and lib.h

//rectangle.h

#ifndef RECTANGLE_H
#define RECTANGLE_H

#include "lib.h"

class RECTANGLE{

POINT* p;
double length;
double width;

};

#endif

//lib.h


#ifndef LIB_H
#define LIB_H

#include <iostream>
using namespace std;

#include "rectangle.h"
#include "point.h"
#include "arrayholder.h"

#endif

In your "rectangle.h" file change "lib.h" to "point.h"

why include point.h instead of lib.h when lib.h has all the definitions needed? your suggestion works, but i just figured out if i just switch point.h and rectangle.h in the lib.h file it also will compile.

Thats what I thought at first but it doesn't work like that and in the .cpp files you have to include "lib.h" and not just its header file or it will do the same thing saying that stuff doesn't exist.

Ok, I understand, thanks

I feel really dumb but for some reason in my main file I can't get the input to be validated, Its either just ending after input or asking for input again no matter how i rearrange the values... and it doesnt matter if the input is correct or not

while( cin>>option ){
    if(option == "PP" || "PR")
      break;
    if(option == "Q")
      exit(0);
  }

if( option == "PP" || "PR" ) is not the same as if( option == "PP" || option == "PR" ) <--this is the one you want

As for the question you asked I don't really know what you mean. Can you post some code?

You answered it, but I'm sure to have more questions, all ive done so far is create the layout for my program i havent even started the plotting of points or output or anything really difficult yet

Just start sending me PMs so this thread doesnt get all cluttered.

I dont know how to make the constructor for arrayholder.h... here are my .h and .cpp files the 2d array a is supposed to hold two values one the x coordinate and one the y coordinate of the points to be plotted

arrayholder.h:

#ifndef ARRAYHOLDER_H
#define ARRAYHOLDER_H

#include "point.h"

class ARRAYHOLDER{

char a[40][40];
POINT* listpointer;

public:
 ARRAYHOLDER(): a[][] , listpointer() {};

 char** initarray();
 void plotpoint();
 void printarray();
};

#endif

// arrayholder.cpp

#include "lib.h"

void initarray(){

  int i, j;

  for(i = 0; i < 40; i++)
    for(j = 0; j < 40; j++)
      a[i][j] = " ";
}

void plotpoint( double x, double y ){
  a[x][y] = '*';
}

void printarray(){

  int i, j;

  for(i = 40; i > -2; i--){

    for(j = 0; j <= 40; j++){
      if(i > 0)
        cout<<i<<a[i][j]<<endl;
      else if( i == 0 && j <= 10 )
        cout<<'0';
      else if( i == 0 && j <= 20 && j > 10 )
        cout<<'1';
      else if( i == 0 && j <= 30 && j > 20 )
        cout<<'2';
      else if( i == 0 && j <= 40 && j > 30 )
        cout<<'3';
      else if( i == -1 )
        cout<<j%10;
    }
  }
}

First major thing I see is that you are putting the function names down like this void initarray(){ when you need to have it in the scope of the class void ARRAYHOLDER::initarray(){ as for your constructor you can do that two ways.

ARRAYHOLDER()//inside the class
{
	//do what you want to a[40][40]
	//do what you want to listpointer
}
//or
ARRAYHOLDER();//inside the class

ARRAYHOLDER::ARRAYHOLDER()//outside of class
{
	//do what you want to a[40][40]
	//do what you want to listpointer
}

//or if you want your constructor to do nothing do not make one
//or if you want to have one constructor with parameters and one without
ARRAYHOLDER(){}//inside the class
ARRAYHOLDER(int x); //inside the class

ARRAYHOLDER::ARRAYHOLDER(int x)
{
	//do what you want to a[40][40]
	//do what you want to listpointer
}

I made a makefile and when i type make i get the error messages:
point.h: In function ‘int main(int, char**)’:
point.h:16: error: ‘double POINT::x’ is private
main.cpp:35: error: within this context
point.h:16: error: ‘double POINT::y’ is private
main.cpp:36: error: within this context
point.h:17: error: ‘POINT* POINT::next’ is private
main.cpp:37: error: within this context
main.cpp:37: error: cannot convert ‘POINT’ to ‘POINT*’ in assignment
point.h:16: error: ‘double POINT::x’ is private
main.cpp:38: error: within this context
point.h:16: error: ‘double POINT::y’ is private
main.cpp:39: error: within this context


How do I go about fixing this/ how do I use those variables if they are private?
here are my main.cpp and point.h files

// main.cpp
#include "lib.h"

int main( int argc, char* argv[] ){

  string option;
  POINT grid;
  POINT temp;

  while(1){
    cout << "PP-Plot a set of points" << endl;
    cout << "PR-Plot a rectangle" << endl;
    cout << "Q-quit\n" << endl;


    while(1){
      cout << "Select an option:" << endl;
      cin>>option;
      if(option == "PP" || option == "PR")
	break;
      if(option == "Q")
	exit(0);
    }

    if(option == "PP"){
      cout << "Enter a list of points" << endl;
      while(1){
	cin >> grid.x;
	cin >> grid.y;
	grid.next = temp;
	cin >> temp.x;
	cin >> temp.y;
    }
    if(option == "PR"){
      cout << "Enter the left corner vertex" << endl;
      cout << "Enter length" << endl;
      cout << "Enter height" << endl;
    }
    if(option == "Q")
      break;
  }
  return 0;

}

// point.h

#ifndef POINT_H
#define POINT_H

#include "lib.h"

class POINT{

  double x, y;
  POINT* next;

 public:
  POINT(): x(0.0), y(0.0), next() {};

  void addPoint( char a[][40], double x, double y );

};

#endif

Make the variables public or make functions that will return the values.

double getX()
{
	return x;
}

and do this for the three variables. (for point you will have to return a POINT*)

I created the functions but that doesnt really help for how to change the variables when input is entered

Oh sorry I thought that said cout.

here.

void setX(double _x)
{
	x = _x;
}

Ok so i did all that now im getting the errors

main.cpp:(.text+0x1fa): undefined reference to `POINT::setX(double)'
main.cpp:(.text+0x21f): undefined reference to `POINT::setY(double)'
main.cpp:(.text+0x231): undefined reference to `POINT::setNext(POINT*)'
main.cpp:(.text+0x256): undefined reference to `POINT::setX(double)'
main.cpp:(.text+0x27b): undefined reference to `POINT::setY(double)'

//main.cpp
#include "lib.h"

int main( int argc, char* argv[] ){

  string option;
  double dtemp;
  POINT grid;
  POINT temp;

  while(1){
    cout << "PP-Plot a set of points" << endl;
    cout << "PR-Plot a rectangle" << endl;
    cout << "Q-quit\n" << endl;


    while(1){
      cout << "Select an option:" << endl;
      cin>>option;
      if(option == "PP" || option == "PR")
	break;
      if(option == "Q")
	exit(0);
    }

    if(option == "PP"){
      cout << "Enter a list of points" << endl;
      while(1){
	cin >> dtemp;
	grid.setX( dtemp );
	cin >> dtemp;
	grid.setY( dtemp );
	grid.setNext( &temp );
	cin >> dtemp;
	temp.setX( dtemp );
	cin >> dtemp;
	temp.setY( dtemp );
    }
    if(option == "PR"){
      cout << "Enter the left corner vertex" << endl;
      cout << "Enter length" << endl;
      cout << "Enter height" << endl;
    }
    if(option == "Q")
      break;
  }
  return 0;

  }
}

//point.h
ifndef POINT_H
#define POINT_H

#include "lib.h"

class POINT{

  double x, y;
  POINT* next;

 public:
  POINT(): x(0.0), y(0.0), next() {};

  void addPoint( char a[][40], double x, double y );
  void setX( double _x );
  void setY( double _y );
  void setNext( POINT* _next );

};

#endif

//point.cpp
#include "point.h"

void POINT::addPoint( char a[][40], int x, int y ){
  a[x][y] = '*';
}

void setX( double _x ){
  x = _x;
}
void setY( double _y ){
  y = _y;
}
void setNext( POINT _next ){
  next = _next;
}

It doesn't really look like you are learning anything because you are missing POINT:: on all the functions when you declare them outside of the class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.