CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Dec 2010
    Posts
    32

    Unhappy Class member initialization help

    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;
    }

  2. #2
    Join Date
    Dec 2010
    Posts
    32

    Re: Class member initialization help

    The errors are from these lines
    Code:
    		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';
    	}

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Class member initialization help

    You're declaring a function without a return type.

  4. #4
    Join Date
    Dec 2010
    Posts
    32

    Re: Class member initialization help

    I meant to use that as a default constructor rather than a function...

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Class member initialization help

    Quote Originally Posted by ertss View Post
    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.
    Code:
    #include <string>
    
    class classinfo
    {
        std::string classname;
        std::string instructor;
        int credits;
        std::string comments;
        char grades;
    
        public:
            classinfo() : 
               classname("No Name"), 
               instructor("No Instructor"),
               credits(0),
               comments("No Comments"),
               grades('U')
               { }
    //...
    };
    
    int main()
    {
       classinfo cInfo;
    }
    Regqrds,

    Paul McKenzie

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Class member initialization help

    Quote Originally Posted by ertss View Post
    I meant to use that as a default constructor rather than a function...
    A constructor has to have the same name as its class.

    FWIW, "Get" functions should return class members. What you call your get functions are actually set functions.
    Last edited by GCDEF; December 23rd, 2010 at 07:53 PM.

  7. #7
    Join Date
    Dec 2010
    Posts
    32

    Re: Class member initialization help

    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...
    Attached Images Attached Images
    • File Type: jpg 1.JPG (27.0 KB, 117 views)

  8. #8
    Join Date
    Dec 2010
    Posts
    32

    Re: Class member initialization help

    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.

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Class member initialization help

    You have a function called ini without a return type.

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Class member initialization help

    Quote Originally Posted by ertss View Post
    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.

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Class member initialization help

    Quote Originally Posted by ertss View Post
    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.

    Regards,

    Paul McKenzie

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Class member initialization help

    Quote Originally Posted by ertss View Post
    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.

  13. #13
    Join Date
    Dec 2010
    Posts
    32

    Re: Class member initialization help

    yes... GCDEF, I know what you mean. I'm now reading C++ Primer, although it seems to be a little bit hard for me...

  14. #14
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Class member initialization help

    Quote Originally Posted by ertss View Post
    yes... GCDEF, I know what you mean. I'm now reading C++ Primer, although it seems to be a little bit hard for me...
    There's a pretty big hump you have to get over before it starts making sense unfortunately.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured