Reputation: 799
When I compile the code below with g++ 4.8.2 I get an error.
#include <iostream>
using namespace std;
class test {
public:
void print() {
cout << str << endl;
}
private:
char str[] = "123456789"; // error: initializer-string for array of chars
// is too long
};
int main() {
char x[] = "987654321";
cout << x << endl;
test temp;
temp.print();
}
Why did I get that error, what is the difference between str
in class test
and x
in main
function?
Upvotes: 5
Views: 10235
Reputation: 17454
I realised what you are asking and rewrite my entire reply. You are not supposed to do you initialization in your class. You only declare the variables/methods you want to use. Only after that, you use your class to create objects.
Set the object's variable's value from then on.
For example:
class Test
{
public:
int num; //You don't intialie value here
};
int main
{
Test testObj();
testObj.num = 100;
}
However, if you set the variables to private, you need to create a function in your class to access the class members. For exmaple setNum()
Alternatively, you can set the variables using the constructor, and insert it as parameter inputs during object creation.
class Test
{
public:
Test(int); //Constructor
private:
int num; //You don't intialie value here
};
Test::Test(int value)
{
this -> num = value;
}
int main
{
Test testObj(100); //set num to 100
}
If you want to access it using class member function, you can do this, but of course you have to define setNum()
in your class first.
testObj.setNum(100);
I know you are asking about char[], but I am giving you example on int. That is not the problem in your code. Either int or char[], you should avoid declaring it directly in your class. It seems like the main problem with your code is not whether you are using char[] or char*. You should not initialize your values in your class.
Upvotes: 1
Reputation: 568
The difference is
class foo {
class y; // Instance Variable
void foo(){
class x; // Method Variable
// use x and y
}
}
An instance variable of a char*
will work char* x = "foobar"
. The compiler possibly allowing the method variable to be set to a char[]
because the scope is limited. Am trying to lookup some literature to explain the different treatment.
Upvotes: 0
Reputation: 42964
Inside your class, you have to explicitly specify the array size:
class test {
...
private:
// If you really want a raw C-style char array...
char str[10] = "123456789"; // 9 digits + NUL terminator
};
Or you can simply use a std::string
(which I think is in general much better in C++ code than using raw C-style strings):
#include <string>
...
class test {
...
private:
std::string str = "123456789";
};
Upvotes: 11
Reputation: 1547
You need to provide the size for char arrays. You can use a pointer instead if you don't know the size of the string or its size is dynamic. If you do this, make sure you don't forget to add a null terminating character.
Upvotes: 0
Reputation: 32914
If you really know that you aren't going to modify the string and is always going to be the same, you can do this instead const char *str = "123456789";
Upvotes: 0
Reputation: 409196
You can't have arrays of unknown size in structures/classes, you need to set an array size.
Or better yet, use std::string
for strings. It's what it's made for.
Upvotes: 2