C++ pointers

for my data structures class

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1249 Views - Last Post: 20 June 2009 - 09:57 PM Rate Topic: -----

#1 circuspeanuts  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 224
  • Joined: 11-April 08

C++ pointers

Posted 12 June 2009 - 12:02 PM

well, I'm back in the C++ section with another question for my fellow much smarter than I crew.

1. Create a class called myclass
2. myclass should have one private member character pointer
3. myclass should have a constructor, destructor, and 3 other public member functions
4. the constructor should allocate memory for the private pointer (size of 25) and alert the user to the fact that memory has been allocated.
5. the first member function(clearstring) should put Null values in the place pointed to by the pointer and alert the user to the fact that the string was properly initialized
6. the second member function(getstring) should prompt the user for a message to load into the string
7. The third member function (displaystring) should display the string that was loaded by the getstring() to the screen
8. the destructor should delete the allocated memory and alert the user to the fact that memory has been released.
9. in the main function instantiate the class as an object and make sure you call all the member functions.


these are the criteria for this first assignment of the quarter, and this is what I have so far.

header
//Header for Unit01 Lab


#include <iostream>
using namespace std;

MyClass::~MyClass
{
	private:
		int *pointer;
}


source file
#include "MyClass.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

#include <iostream>
using namespace std;

class MyClass
{
	pointer = new int[25];
	cout << "Memory has been allocated for the pointer";
}

void clearString()
{
	memset(pointer, '/0', sizeof(pointer);
	cout << "The string has been properly initialized";
}

void getString()
{
	cout << "Please leave a message";
	cin >> *pointer;
}

void displayString()
{
	coud << *pointer;
}

int main()
{
	
	return EXIT_SUCCESS;
}




I have 1 error that I'm trying to work out on my own

Quote

(15) : fatal error C1001: INTERNAL COMPILER ERROR
(compiler file 'msc1.cpp', line 1786)
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information
Error executing cl.exe.



my only other question is how do I get the display to show in the display string member function, and to delete it?

thanks,
Cp

This post has been edited by circuspeanuts: 12 June 2009 - 12:05 PM


Is This A Good Question/Topic? 0
  • +

Replies To: C++ pointers

#2 Dantheman  Icon User is offline

  • D.I.C Regular

Reputation: 34
  • View blog
  • Posts: 445
  • Joined: 27-May 09

Re: C++ pointers

Posted 12 June 2009 - 12:33 PM

Your question doesn't make much sense to me. What exactly do you want to display and what exactly do you want to delete.

Obviously, I'm not going to do your homework. But I'll point you in the right direction.
Was This Post Helpful? 0
  • +
  • -

#3 Kanvus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 452
  • Joined: 19-February 09

Re: C++ pointers

Posted 12 June 2009 - 07:41 PM

Your class is written with improper syntax. Your header only contains a destructor. It's supposed to have class MyClass instead of it being in the .cpp

It should look like the example on this page somewhat http://www.learncpp....d-header-files/
Was This Post Helpful? 0
  • +
  • -

#4 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Re: C++ pointers

Posted 12 June 2009 - 11:56 PM

Damn, it seems like people that are self taught know better programming than people that learn it in school.
Was This Post Helpful? 0
  • +
  • -

#5 circuspeanuts  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 224
  • Joined: 11-April 08

Re: C++ pointers

Posted 15 June 2009 - 07:22 AM

basically this is my updated code now:

header
#include <iostream>
using namespace std

class MyClass
{
	private:
		char *pointer1;
}



source
#include "MyClass.h"
#include <iostream>
using namespace std;

MyClass::MyClass()
{
	*pointer1 = new char[25];
	cout << "Memory has been properly allocated" << endl;
}

void clearString()
{
	memset(pointer, '/0', sizeof(*pointer1);
	cout << "The string has been properly initialized" << endl;
}

void getString()
{
	cout << "Please leave a message" << endl;
	cin >> *pointer1;
}

void displayString()
{
	cout << *pointer1;
}

int main()
{
	return EXIT_SUCCESS;
}



MyClass::~MyClass()
{
	
}



I guess what I'm asking is am I doing this right in accordance with what my instructor wishes. I haven't done C++ in around 5 -6 months, so it's kind of my fault, and I'm trying to go by memory.


and I have these errors now.

Quote

Unit01 Lab\MyClass.cpp(10) : error C2065: 'pointer1' : undeclared identifier
Unit01 Lab\MyClass.cpp(10) : error C2440: '=' : cannot convert from 'char *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
Unit01 Lab\MyClass.cpp(16) : error C2065: 'pointer' : undeclared identifier
Unit01 Lab\MyClass.cpp(16) : error C2100: illegal indirection
Unit01 Lab\MyClass.cpp(16) : error C2143: syntax error : missing ')' before ';'
Unit01 Lab\MyClass.cpp(23) : error C2100: illegal indirection
Unit01 Lab\MyClass.cpp(28) : error C2100: illegal indirection



kind of puzzling to me..

I declared my pointer (*pointer1) in my header file, but cannot get it passed through to the constructor in my source file to allocate it properly. along with the other errors..

and I'm not trying to convert from char to int..

I'm thinking I may have to use a for loop to clear the string and enter null values into the pointer?

This post has been edited by circuspeanuts: 15 June 2009 - 07:28 AM

Was This Post Helpful? 0
  • +
  • -

#6 Kanvus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 452
  • Joined: 19-February 09

Re: C++ pointers

Posted 15 June 2009 - 08:18 AM

I fixed your errors. Compare them with your code to see obvious mistakes. If something is not obvious, I'll gladly point every mistake out if you care to learn from them more in depth.

MyClass.h
class MyClass
{
public:
	MyClass();
	~MyClass();

	void clearString();
	void getString();
	void displayString();

private:
		char* pointer1;
};



MyClass.cpp
#include <iostream>
#include "MyClass.h"

using namespace std;

MyClass::MyClass()
{
	pointer1 = new char[25];
	cout << "Memory has been properly allocated" << endl;
}

void MyClass::clearString()
{
	memset(pointer1, '/0', sizeof(pointer1));
	cout << "The string has been properly initialized" << endl;
}

void MyClass::getString()
{
	cout << "Please leave a message" << endl;
	cin >> *pointer1;
}

void MyClass::displayString()
{
	cout << *pointer1;
}

MyClass::~MyClass()
{
	delete[] pointer1;
}


main.cpp
#include <iostream>
#include "MyClass.h"

using namespace std;

int main()
{
	MyClass hiMom; //Properly allocated memory

	system("PAUSE"); //So console doesn't close
	return EXIT_SUCCESS;
}

Was This Post Helpful? 1
  • +
  • -

#7 circuspeanuts  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 224
  • Joined: 11-April 08

Re: C++ pointers

Posted 15 June 2009 - 08:31 AM

oook... but even so I still have 8 errors now, which is more than I had?

Quote

unit01lab\myclass.cpp(11) : error C2065: 'pointer1' : undeclared identifier
unit01lab\myclass.cpp(11) : error C2440: '=' : cannot convert from 'char *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
unit01lab\myclass.cpp(15) : error C2039: 'clearString' : is not a member of 'MyClass'
unit01lab\myclass.h(13) : see declaration of 'MyClass'
unit01lab\myclass.cpp(21) : error C2039: 'getString' : is not a member of 'MyClass'
unit01lab\myclass.h(13) : see declaration of 'MyClass'
unit01lab\myclass.cpp(24) : error C2100: illegal indirection
unit01lab\myclass.cpp(27) : error C2039: 'displayString' : is not a member of 'MyClass'
unit01lab\myclass.h(13) : see declaration of 'MyClass'
unit01lab\myclass.cpp(29) : error C2100: illegal indirection
unit01lab\myclass.cpp(34) : error C2541: delete : cannot delete objects that are not pointers
Main.cpp
Error executing cl.exe.


I don't understand that whole bit.

if you wouldn't mind though, could you explain to me the errors I made in my code? I think I understand them, like not declaring my member functions in my header file, but some of the others I'm not sure I understand.
Was This Post Helpful? 0
  • +
  • -

#8 Kanvus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 452
  • Joined: 19-February 09

Re: C++ pointers

Posted 15 June 2009 - 08:38 AM

It compiled for me. Did you wipe out everything you have and use ONLY my code? It looks like you're mixing new with old. Please start anew.

And some of your errors.

You did

char* pointer1;

then

*pointer1 = new char[25];


When, pointer1 is correct, not *pointer1. The * is used at declaration only. If used again, it dereferences it into a char instead of pointer. The pointer form would be pointer1. In memset, your first argument was pointer, forgot the 1.

To clear memory of a new, you need delete. And a char* pointer is an array. delete[] is the delete for arrays.

EDIT: Your displayString will only display the first character, pointer1[0]. Please use a for loop like you said to get the whole thing.

This post has been edited by Kanvus: 15 June 2009 - 08:46 AM

Was This Post Helpful? 0
  • +
  • -

#9 circuspeanuts  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 224
  • Joined: 11-April 08

Re: C++ pointers

Posted 15 June 2009 - 08:50 AM

ah ok it works. What happened was I was creating a class called MyClass inside of my project name, which was making 2 classes nested and then when I would try to make my header file it would tell me that it already had a header file called MyClass.h because of the nested classes. So I started over, and instead of right clicking and making a new class I just made source files, although we're supposed to have a class called MyClass, so in making that, and inputing my information in that file will not mess it up, correct? or is it fine the way it is with the myclass file there?


also, now the only thing that I have is the declaration of the member functions in the main.cpp, which I assume is himom. so in calling my functions it would basically be along the lines of
himom::clearString




tested.


when using a single semicolon it tells me that my member functions are undeclared identifiers.
when using a period my function call is missing an argument list
correct?


errors went away, but does not call the functions to the console for input.

This post has been edited by circuspeanuts: 15 June 2009 - 08:58 AM

Was This Post Helpful? 0
  • +
  • -

#10 circuspeanuts  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 224
  • Joined: 11-April 08

Re: C++ pointers

Posted 15 June 2009 - 09:03 AM

instructor just told me that it has to be dynamic, not static...
Was This Post Helpful? 0
  • +
  • -

#11 Kanvus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 452
  • Joined: 19-February 09

Re: C++ pointers

Posted 15 June 2009 - 09:08 AM

hiMom.getString();
hiMom.displayString(); 


Dynamic I assume means using the .

:: are called colons, two of them. semicolon is this ;

:: is used for static

so use the period .
Was This Post Helpful? 0
  • +
  • -

#12 circuspeanuts  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 224
  • Joined: 11-April 08

Re: C++ pointers

Posted 15 June 2009 - 09:13 AM

I was using the period .

he said the dynamic "Version" was using the heap and not the stack. the way it is now I'm taking off of the stack, which he deems to be a very bad thing as it limits the ability in terms of memory and such. which means according to him I would have to incorporate the "new" variable to make off of the heap. something along the lines of:


MyClass ohiMom = new MyClass;




correct?

that does not work...


also, I haven't implemented a for loop yet. I haven't done this in quite some time remember...

This post has been edited by circuspeanuts: 15 June 2009 - 09:17 AM

Was This Post Helpful? 0
  • +
  • -

#13 Kanvus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 452
  • Joined: 19-February 09

Re: C++ pointers

Posted 15 June 2009 - 09:20 AM

int main()
{
	MyClass* hiMom = new MyClass; //Properly allocated memory

	hiMom[0].getString();
	hiMom[0].displayString();

	system("PAUSE"); //So console doesn't close
	return EXIT_SUCCESS;
} 


Needs a pointer. Want more?

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

using namespace std;

int main()
{
	MyClass* hiMom = new MyClass[50]; //Properly allocated memory

	hiMom[0].getString();
	hiMom[0].displayString();

	hiMom[3].getString();
	hiMom[3].displayString();

	system("PAUSE"); //So console doesn't close
	return EXIT_SUCCESS;
}

This post has been edited by Kanvus: 15 June 2009 - 09:23 AM

Was This Post Helpful? 0
  • +
  • -

#14 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++ pointers

Posted 15 June 2009 - 09:29 AM

Couple of things regarding Kanvus' post:

1. You probably only need one object of MyClass, i could be wrong, if you are, you need to call the constructor properly: MyClass* theClass = new MyClass(); //do not forget parentheses

2. Kanvus, you aren't freeing memory you explicitly allocated in main, you are leaking memory. While the class may clean up after itself in the destructor, nothing is cleaning up after main, don't forget to call delete ot delete[] depending on whether or not you made an array of objects.

3. What is this:

 hiMom[0].getString();
 hiMom[0].displayString();



No no no. This is an object, a single one, you do not need subscript operators to do stuff with it. This is terrible advice (mainly because I don't know anyone who does it like that). If you're going to do that you might as well dereference rather then subscript:

(*hiMom).methodName();



edit:

Possible corrections, the proper way to do it:

int main()
{
   // MyClass* hiMom = new MyClass; //inncorrect
	MyClass* hiMom = new MyClass(); //correct

	hiMom->getString();
	hiMom->displayString(); //correct, do NOT use [] operators on singleton objects

	system("PAUSE"); //So console doesn't close
	 delete hiMom; //free the memory
	return EXIT_SUCCESS;
}


This post has been edited by KYA: 15 June 2009 - 09:33 AM

Was This Post Helpful? 1
  • +
  • -

#15 Kanvus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 452
  • Joined: 19-February 09

Re: C++ pointers

Posted 15 June 2009 - 09:33 AM

thanks. did everything in a matter of minutes so my fresh perspective is dimming. good catch

fix to string output
void MyClass::displayString()
{
	for( int i = 0; i < strlen(pointer1); i++ )
			cout << pointer1[i];

	cout << endl;
}

This post has been edited by Kanvus: 15 June 2009 - 09:35 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2