Teen Programmers Unite  
 

 

Return to forum top

Trouble with seperating interface from implementation

Posted by CodeRed [send private reply] at May 07, 2002, 08:17:59 PM

Okay I've never seperated a class like this but I have to for a project. I'm talking about C++ of course, about seperating the class interface from it's implementation, the process of abstraction basically. The problem I have is that when I try to create an object using the class it tells me that the constructor cannot access the private variables. Here is a simplified version of what I have to do and the problem I am having.

Box.h:

#ifndef BOX
#define BOX

class Box
{
private:

	int heigth, width, perimeter;
	float area;

	Box();
	Box(int h, int w);

public:

	int getPerim();
	float getArea();
};

#endif




Box.cpp
#include "Box.h"

Box::Box(){}

Box::Box(int h, int w)
{
	heigth = h;
	width = w;
}

int Box::getPerim()
{
	return heigth+heigth+width+width;
}

float Box::getArea()
{
	return (float)heigth*width;
}


main.cpp
#include <iostream>
using namespace std;

#include "Box.h"

void main()
{
	Box box1(10,10);
}




It compiles and builds okay but I get this error when I try to run it:
"error C2248: 'Box::Box' : cannot access private member declared in class 'Box....see declaration of 'Box::Box'"
Any Ideas?
Posted by Psion [send private reply] at May 07, 2002, 08:43:44 PM

This has nothing to do with "separating blah blah blah". You can never access private members of a class object from outside that class.

Posted by CodeRed [send private reply] at May 07, 2002, 08:53:46 PM

Want me to copy an excerpt from bruce eckells "Thinking in C++ 2nd Edition" that demonstrates exactly what I am talking about and shows that it is possible, it's just that I can't get it to work

Posted by CodeRed [send private reply] at May 07, 2002, 09:08:12 PM

Here:

//: C07:Mem.h
#ifndef MEM_H
#define MEM_H
typedef unsigned char byte; 
class Mem 
{
  byte* mem;
  int size;
  void ensureMinSize(int minSize);
public:
  Mem();
  Mem(int sz);
  ~Mem();
  int msize();
  byte* pointer();
  byte* pointer(int minSize);
};
#endif // MEM_H ///:~



//: C07:Mem.cpp {O}
#include "Mem.h"
#include <cstring>
using namespace std; 
Mem::Mem() 
{ 
  mem = 0; 
  size = 0; 
} 
Mem::Mem(int sz) 
{
  mem = 0;
  size = 0;
  ensureMinSize(sz);
} 

Mem::~Mem() 
{ 
  delete []mem; 
} 

int Mem::msize() 
{ 
  return size; 
} 

void Mem::ensureMinSize(int minSize) 
{
  if(size < minSize) 
  {
    byte* newmem = new byte[minSize];
    memset(newmem + size, 0, minSize - size);
    memcpy(newmem, mem, size);
    delete []mem;
    mem = newmem;
    size = minSize;
  }
} 

byte* Mem::pointer() 
{ 
return mem; 
} 

byte* Mem::pointer(int minSize) 
{
  ensureMinSize(minSize);
  return mem;
} 


As you can see in the mem.cpp he has the implementation of the constructor like this:

Mem::Mem(int sz)
{
mem = 0;
size = 0;
ensureMinSize(sz);
}

IN WHICH he is accessing the private variable "size" of the mem class which is defined in mem.h. He is accessing the private variables of a class not only outside of the class but in another file entirely, the implementation, or cpp, file. See this is called abstraction, or, as you put it "seperating blah blah blah" It means putting the definition of a class in one file (the .h, or interface file) while putting the actuall implementation of it's member function in another file (the .cpp, or implementation file)
Now, can anyone tell me why my code isn't working?


Posted by Psion [send private reply] at May 07, 2002, 10:44:22 PM

No, you are still the confused one. Bruce has made his constructors public, while you have somehow decided that you can make them private and still call them from outside the class.

Posted by taubz [send private reply] at May 07, 2002, 10:45:07 PM

<<He is accessing the private variables of a class not only outside of the class but in another file entirely>>

He's accessing it within the same class (that is, within the definition of class Box), but in a separate file. The file has nothing to do with it.

How are you getting this error at runtime?

- taubz

Posted by gian [send private reply] at May 07, 2002, 11:33:32 PM

Your constructor is private! It needs to be public!

box.h should read something like:

#ifndef BOX
#define BOX

class Box
{
private:

	int heigth, width, perimeter;
	float area;

public:

	int getPerim();
	float getArea();
        Box();
	Box(int h, int w);
};

#endif
Posted by CodeRed [send private reply] at May 08, 2002, 12:24:52 AM

Oh, thanks. But that wasn't my problem. In the code that I am writing I was doing something different. Making the constructor private in this little box example was a stupid mistake, but I figured out what I was doing wrong in my project, so everything is cool.

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.