Hi all,
I was trying to build a class that includes the info about a specific course taken.
I tried to initialize all the class members using constructor... But I got some errors like:
1>d:\my documents\visual studio 2010\projects\classinfo\classinfo\main.cpp(14): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\my documents\visual studio 2010\projects\classinfo\classinfo\main.cpp(20): warning C4183: 'ini': missing return type; assumed to be a member function returning 'int'
1>d:\my documents\visual studio 2010\projects\classinfo\classinfo\main.cpp(16): error C2059: syntax error : '{'
1>d:\my documents\visual studio 2010\projects\classinfo\classinfo\main.cpp(16): error C2143: syntax error : missing ';' before '{'
1>d:\my documents\visual studio 2010\projects\classinfo\classinfo\main.cpp(16): error C2143: syntax error : missing ';' before '}'
Could anyone help me on that? Thanks a lot in advance...
Here is my code:
Code:
#include <iostream>
#include <string>
using namespace std;
class classinfo{
private:
string classname;
char instructor[15];
int credits;
string comments;
char grades;
public:
ini(){
classname="No Name";
instructor[15]={'N','O',' ','I','N','S','T','R','U','C','T','O','R','\0'};
credits=0;
comments="No Comments";
grades='U';
}
void getclass(string cstring){
classname=cstring;
}
void getinstructor(char* istring){
for(char *ibegin=instructor,*iend=instructor+15;ibegin!=iend;++ibegin){
*ibegin=*istring;
++istring;
}
}
void getcredits(int credit){
credits=credit;
}
void getcomments(string comment){
comments=comment;
}
void getgrades(char grade){
grades=grade;
}
void printclass(){
cout<<"Class Name: "<<classname<<endl;
cout<<"Class Instructor: ";
for(char *ibegin=instructor,*iend=instructor+15;ibegin!=iend;++ibegin)
cout<<*ibegin;
cout<<endl;
cout<<"Class credits: "<<credits<<endl;
cout<<"Class Comments: "<<comments<<endl;
cout<<"Class grade: "<<grades<<endl;
}
};
int main()
{
string temp;
char temp1[15]="Hal Carter";
char* p=temp1;
int cre;
char g;
classinfo class1,class2;
cout<<"The class info default is"<<endl;
class1.printclass();
cout<<"Please input the class name of your first class"<<endl;
cin>>temp;
class1.getclass(p);
cout<<"Please input the credits of your first class"<<endl;
cin>>cre;
class1.getcredits(cre);
cout<<"Please input the comments of your first class"<<endl;
cin>>temp;
class1.getcomments(temp);
cout<<"Please input the grade of your first class"<<endl;
cin>>g;
class1.getgrades(g);
cout<<"The class info:"<<endl;
class1.printclass();
cin.get();
return 0;
}
I meant to use that as a default constructor rather than a function...
If you quit using char arrays and use std::string, then you could accomplish your goal.
The instructor type should be a std::string. What if the instructor name is more than 15 characters? Why did you decide to use a char array for the instructor name, but everything else uses std::string? Be consistent -- if it's a string, use std::string. Bouncing back and forth between char arrays and std::string, and the purpose of both is to store a name makes no sense.
I still cannot figure out what's the problem...
I think I must have misused the default constructor... I did it following a youtube tutorial...
I attached the code from the tutorial and could you guys show me my fault when applying this kind of
scheme?
Paul, I don't think it's because of the use of char[] or string type;
Because the same error still pointed to the constructor usage after I changed the type.
Paul, I don't think it's because of the use of char[] or string type;
Because the same error still pointed to the constructor usage after I changed the type.
It is the reason.
You want to initialize variables, you do that in the constructor initialization list. The name of the constructor must be the same name as the class.
Your class name is "classinfo", but you then have a function called "ini", which is not a constructor. Look at the code I posted -- there are no errors. I correctly coded a constructor that initializes your variables.
Secondly, arrays cannot be initialized unless they are declared locally and initialized. You cannot have an array as a member variable and initialize it in the way you're attempting to do. Arrays are not "smart" and do not comprehend C++ initialization syntax in the inititalization list. Therefore, drop the array and use std::string.
Last, ini() is not a constructor, it is a member function. Member functions must have a return type specified, and you did not do that. GCDEF already pointed this out to you.
I still cannot figure out what's the problem...
I think I must have misused the default constructor... I did it following a youtube tutorial...
Forget the tutorial -- look at the code I posted. It compiles and initializes those variables.
I attached the code from the tutorial and could you guys show me my fault when applying this kind of scheme?
What that tutorial is showing you is how to assign to variables after they have been initialized. It does not explain the difference between initialization and assignment.
To initialize variables, you use the initialization-list -- again, see my post and you will see the difference. Also, I suggest you get books, as tutorials can come from anywhere and from anyone claiming to be teaching C++, when they/he/she/it may not know the language properly enough to be teaching it. Unless the tutorial has been peer-reviewed by other C++ programmers, you are learning at your own risk.
I still cannot figure out what's the problem...
I think I must have misused the default constructor... I did it following a youtube tutorial...
I attached the code from the tutorial and could you guys show me my fault when applying this kind of
scheme?
Thanks for all your advices...
I'll add one more thing. IMHO, your chances of learning C++ programming from YouTube are 0. There's just way too much you have to know. Get a good book.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.