class student
{
public:
char name[];
int age;
};
int main()
{
student q;
q.name[]="Jim"; // This is the line with the error
q.age=19;
cout<<q.name<<q.age;
getch();
return 0;
}
The error is as follows:
12 D:\Trial Programs\Chumma.cpp expected primary-expression before ']' token
The first thing is that, unless im mistaken, the "[]" at the end of "name[]" makes it an array, of an undefined size and the error is asking you to say which number in the array it is. i just put "1" inside of both braces(? not sure if they are braces or brackets?) it accept that, but it also doesnt accept "Jim" as a char for a reason im not even going to pretend i know enough to explain, so i put #include <string> at the top, and made "name" a string instead of a char and it worked just fine after that. heres what i came up with:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include <string>
class student
{
public:
std::string name[1];
int age;
};
int main()
{
student q;
q.name[1]="Jim"; // Error fixed.
q.age=19;
std::cout<<q.name<<q.age;
std::cin.ignore();
return 0;
}
that is the error that i dont know enough to explain, so i switched it from a "char" variable to a "string". also you wouldnt need an array of 10 for this, that would create 10 "slots" for names to be put in e.g.