0

Hi anyone.
Please help with this seperate compilation.

Many thanks !
==================================

//pointerDataClass ;Header file

class pointerDataClass
{
public:
    void print() const;
    //Function to output the value of x and
    //the value of the array p.
    
    void setData();
    //Function to input data into x and
    //into the array p.
    
    void destroyP();
    //Function to deallocate the memory space
    //occupied bt the array p.
    
    pointerDataClass(int sizeP = 10);
    //constructor
    //Create an array of the size specified by the
    //parameter sizeP; the default array size is 10.
    
    ~pointerDataClass();
    //destructor
    //Deallocate the memory space occupied by the array p.
    
    pointerDataClass (const pointerDataClass& otherObject);
    //copy constructor
    
private:
    int x;
    int lenP;
    int *p;        //pointer to an int array
};

//pointerDataClassIMP ; this is the implementation file

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

void pointerDataClass::print() const
{
    cout << "x = " << x << endl;
    cout << "p = ";
    
    for (int i = 0; i < lenP; i++)
        cout << p[i] << " ";
    cout << endl;
}

void pointerDataClass::setData()
{
    cout << "Enter an integer for x: ";
    cin >> x;
    cout << endl;
    
    cout << "Enter " << lenP << " numbers: ";
    
    for (int i = 0; i < lenP; i++)
        cin >> p[i];
    cout << endl;
}

void pointerDataClass::destroyP()
{
    lenP = 0;
    delete [] p;
    p = NULL;
}

pointerDataClass::pointerDataClass(int sizeP)
{
    x = 0;
    
    if (sizeP <= 0)
    {
      cout << "Array size must be positive." << endl;
      cout << "Creating an array of size 10." << endl;
      
      lenP = 10;
    }
    else
      lenP = sizeP;
      
    p = new int[lenP];
    assert(p != NULL);
}

pointerDataClass::~pointerDataClass()
{
    delete [] p;
}

//copy constructor
pointerDataClass::pointerDataClass(const pointerDataClass& otherObject)
{
    x = otherObject.x;
    
    lenP = otherObject.lenP;
    p = new int[lenP];
    assert(p != NULL);
    
    for (int i = 0; i < lenP; i++)
        p[i] = otherObject.p[i];
}
#include <iostream>
#include "pointerDataClass.h"
using namespace std;

int main() {

    pointerDataClass regClass;
    
    regClass.setData();
    regClass.print();
  
  return 0;
}

The compiler is pointing to line 2 in Main.
Why?
Am i not including the header file correctly ??
Please advise.

3
Contributors
11
Replies
12
Views
9 Years
Discussion Span
Last Post by henpecked1
0

This is the error message in the compile log:

Compiler: Default compiler
Building Makefile: "C:\unisa\devcpp\Makefile.win"
Executing make...
gmake.exe -f "C:\unisa\devcpp\Makefile.win" all
Execution terminated

0

Don't have any *.O files in my project directory

0

Please refer to the attachment to see whether there are files in my project directory.

Thanks.

Attachments filetypes.JPG 110.02 KB
0

Will you zip up that directory and attach it so that I can check it out? My guess is that you didn't make the project correctly. But that is just a guess.

0

Ancient Dragon .. i have to run will be back !!
Thanks for your help thus far.
I'll reply when i'm back online again.

0

all you had to do was add using statements at the top of the program, and it compiled/linked ok for me.

//pointerDataClassIMP       ; this is the implementation file
#include <iostream>
#include "pointerDataClass.h"
using std::cout;
using std::endl;
using std::cin;
0

I have a small question, why do you use the void destroyp function when you have a class destructor that appears to do the same thing?

And not to beat a dead horse as AD is an expert, but I too saw the including of namespace in the main, but not in the class implementation

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.