Question:
C++: strings in classes?
Questochango
2012-02-04 13:30:58 UTC
I am making a class but for some reaosn it will not allow me to use strings. Here is what I have so far:


#ifndef STUDENT_H
#define STUDENT_H
#include


class Student
{
private:
char name [30];
int numClasses;
string str1 = "hello";
};

#endif

But whenever I compile it tells me there is a missing type identifier on the line with string. I know I am missing something ridiculously simple can someone please help?
Five answers:
?
2012-02-04 15:06:19 UTC
to make the array dynamic



char *name = new char[30]



i dont see why you would need to make the array dynamic though, the static array should work fine





READ THIS: http://www.cplusplus.com/doc/tutorial/dynamic/
husoski
2012-02-04 15:21:52 UTC
You already found the answer to the std::string problem. I'd recommend using the second approach, since using declarations in a header file can pollute the global namespace of the calling program. Just type the std:: prefixes. You get used to it, eventually.



That [str1 = "hello";] initialization of a member isn't legal. Java allows that, but not C++. If you want an initial value other that what the default constructor for string() provides (an empty string, I believe) then you need a constructor for your class:



// in the class declaration:

public:

Student();



// in the class implementation:

Student::Studen() : str1("Hello)

{

}



That's it.





The dynamic array needs initialization code to allocated it, and a destructor to make sure it gets deallocated when the object is destroyed. If it didn't look like a C string, I'd say to use a std::vector type and let the vector class handle allocation and destruction. You still need to specify the size somewhere, but that can happen either in the constructor or when you first put data in the member. That should be through a method call, so you already have "lines of code" for that anyway. It's a good idea to set it to NULL if you don't have it defined yet:



// declaration:

public:

char *name;

int nameSize;



// modified constructor:

Student::Student() : name(NULL), nameSize(0), str1("Hello")

{

}



Anyway, if you want to use C string functions on that char[] array, declare name as a char* instead and use name = new char[size]; to create the array, and delete[] name; to release it when you're done. You should also store the current size of a dynamic array someplace so you'll be able to test on method calls whether the allocated size is big enough to handle a requested operation. Then you need a destructor for Student. It's simple enough to define in-class:



public:

~Student() { if (name != NULL) delete[] name; name = NULL; }



That's a good pointer management idea. Set a pointer to NULL intially, set it non-NULL when it points to something, and set it *back* to NULL when it doesn't point to anything any more. In this case, that lets your destructor know whether it needs to delete anything. Also not delete[], not just delete. If you allocate a pointer as a new array, you must use delete[] to free it.



PS: To use the NULL macro, or must be included somewhere. 0 can be used instead, but NULL makes it clearer that the value is intended to be a pointer rather than a number.
?
2016-12-09 07:09:22 UTC
C would not have training. It in basic terms had a convention. To make an array of characters and end the string with a nil byte. additionally there have been purposes to function on strings yet they have been self sufficient. C++ honestly has each little thing encapsulated and the convention of how the string data is controlled can get replaced at any time and not consequence person code. So the class manages itself.
2012-02-04 15:44:06 UTC
Well, you're declaring the string as with the private access specifier. This means that ONLY the functions/methods in the class are able to make a change to the private members. You could try declaring str1 as public, but I'm not sure that ansi-C++ allows it. Solution? Make a default constuctor and set str1 as "hello" in the default constructor.



Also, you're missing using namespace std
Ratchetr
2012-02-04 13:40:19 UTC
string is in the std namespace.

So you either need:

using namespace std; somewhere before the class declaration.



Or use std::string.



Also, you can't assign a value to str1 here. You will need to do that in your constructor.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...