C++ Copy Constructor Problems!

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 1560 Views - Last Post: 05 August 2010 - 03:51 PM Rate Topic: -----

#1 Guest_Curtis*


Reputation:

C++ Copy Constructor Problems!

Posted 05 August 2010 - 09:40 AM

Hi there all, My name is Curtis and im having a little bit of trouble with my code, would someone mind taking a look at it for me and help me out??
Thanks a lot

Regards

Description:
must use C++ style classes
class’ state must use dynamically allocated arrays
must Include appropriate member functions for object construction and destruction

class cMESSAGE
{
    private :
    char* message;
    int lenght;

    public :
    void* XOR(char, int);
    void* CAESAR(char, int);
   
    cMESSAGE();
    ~cMESSAGE();

};
// classs object
cMESSAGE encryption;

cMESSAGE :: cMESSAGE()
{
    encryption.lenght = NULL;
    encryption.message = NULL;

}

cMESSAGE :: ~cMESSAGE()
{
    encryption.lenght;
    encryption.message;
}



Is This A Good Question/Topic? 0

Replies To: C++ Copy Constructor Problems!

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: C++ Copy Constructor Problems!

Posted 05 August 2010 - 09:42 AM

Where is your copy constructor? What specific problems are you having with it?
Was This Post Helpful? 0
  • +
  • -

#3 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: C++ Copy Constructor Problems!

Posted 05 August 2010 - 09:58 AM

Im assumming you are trying to DEEP copy your char *message within your constructor

There are two types of coping

DEEP COPY - Actually copying thing memory to a new memory location
SHALLOW COPY - Pointing a pointer to an existing memory location


DEEP COPY example
class Foo
{
	char *message;
	
	public:
	Foo(char *msg)
	{
		unsigned int size = strlen(msg);
		message = new char[size+1];  //strlen does not count the NULL character, therefor we add 1
		for(int i=0; i<size; i++)
			message[i] = msg[i];
		message[size] = '\0'; //set NULL char
	}
};
	


Was This Post Helpful? 0
  • +
  • -

#4 Guest_Curtis*


Reputation:

Re: C++ Copy Constructor Problems!

Posted 05 August 2010 - 11:59 AM

Thank you so much, i will try that method now and il let you know.
Thanx again :) much appreciated
Was This Post Helpful? 0

#5 Guest_Curtis*


Reputation:

Re: C++ Copy Constructor Problems!

Posted 05 August 2010 - 12:14 PM

I keep getting this error


201003757_P02\Class.h|6|error: `char*cMESSAGE::message' is private|

and may I ask how I would call "message" in main through my class to all the required data to it?

Regards
Was This Post Helpful? 0

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: C++ Copy Constructor Problems!

Posted 05 August 2010 - 12:28 PM

Make it public?
Was This Post Helpful? 0
  • +
  • -

#7 Guest_Curtis*


Reputation:

Re: C++ Copy Constructor Problems!

Posted 05 August 2010 - 12:31 PM

201003757_P02\Class.h|11|error: invalid use of non-static data member `cMESSAGE::lenght'|


Wow I really dont know how to call message into main to I can add a string to It
Was This Post Helpful? 0

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: C++ Copy Constructor Problems!

Posted 05 August 2010 - 12:42 PM

updated code listing please
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: C++ Copy Constructor Problems!

Posted 05 August 2010 - 12:54 PM

I'm going to guess that you're calling lenght like this:
cMESSAGE::lenght()

That's not how you do it. You need to read your textbook on how to use classes. You create an instance of a class, which is your object. You then call methods of that object, not of the class.

I think you need to read more about how this all works, rather than running to us with every little error.

EDIT: Try reading this tutorial also.

This post has been edited by JackOfAllTrades: 05 August 2010 - 12:57 PM

Was This Post Helpful? 0
  • +
  • -

#10 Guest_Curt*


Reputation:

Re: C++ Copy Constructor Problems!

Posted 05 August 2010 - 12:55 PM

Hi there all, now I have a new Error, but I fixed the others:
201003757_P02\main.cpp|27|error: cannot call member function `void* cMESSAGE::XOR(char, int)' without object|

Comes from this Line of code
cMESSAGE::XOR(cMESSAGE::mess, cMESSAGE::lenght);

Was This Post Helpful? 0

#11 Guest_Curtis*


Reputation:

Re: C++ Copy Constructor Problems!

Posted 05 August 2010 - 12:59 PM

Oh okay then, umm I'm terribly sorry for inconveniencing you all, I just did not understand and needed someones help, not a textbook.
Again I am sorry for troubling you all,

Kindest Regards

View PostJackOfAllTrades, on 05 August 2010 - 11:54 AM, said:

I'm going to guess that you're calling lenght like this:
cMESSAGE::lenght()

That's not how you do it. You need to read your textbook on how to use classes. You create an instance of a class, which is your object. You then call methods of that object, not of the class.

I think you need to read more about how this all works, rather than running to us with every little error.

Was This Post Helpful? 0

#12 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: C++ Copy Constructor Problems!

Posted 05 August 2010 - 01:04 PM

Curtis, you're doing two things wrong here:

1. You're just throwing code together without understanding it. I'm trying to tell you that's the WRONG WAY to do it.

2. You're just throwing your errors up here with every new compilation with NO CODE to show us what you're doing.

If you want good help, you've got to put in an effort as well.
Was This Post Helpful? 0
  • +
  • -

#13 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: C++ Copy Constructor Problems!

Posted 05 August 2010 - 01:15 PM

You need to create an instance of your class before you start making function calls (Unless there static functions - which their not)


#include <iostream>

class Foo
{
	char *message;
	
	public:
	Foo(char *msg)
	{
		unsigned int size = strlen(msg);
		message = new char[size+1];  //strlen does not count the NULL character, therefor we add 1
		for(int i=0; i<size; i++)
			message[i] = msg[i];
		message[size] = '\0'; //set NULL char
	}
};

int main()
{
    Foo bob("This is Bobs Message"); //created an instance of my class and called the constructor
    Foo jim("This is Jims Message"); //created another instances of my class

    return 0;
}



[/code]

This post has been edited by ImaSexy: 05 August 2010 - 01:17 PM

Was This Post Helpful? 0
  • +
  • -

#14 Guest_Curtis*


Reputation:

Re: C++ Copy Constructor Problems!

Posted 05 August 2010 - 01:34 PM

Hey,
I know and I am sorry, I completely understand what u are saying, it is quite a bit of code tho so it would be a lot easier if I could just email my entire program to someone its just one or two small things I cant seem too get my head around and fix, I created the object for the class and everything.

Regards
Was This Post Helpful? 0

#15 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10471
  • View blog
  • Posts: 38,809
  • Joined: 27-December 08

Re: C++ Copy Constructor Problems!

Posted 05 August 2010 - 01:37 PM

You are welcome to post your code and errors here, but please keep all work via the forums rather than email so that everyone can benefit. :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2