Help with syntax error (constructor)

This is a discussion on Help with syntax error (constructor) within the C++ Programming forums, part of the General Programming Boards category; Code: student::student(string name,float g) { cout <<"student constructor is running..." <<endl; student.sname=name; //problem line gpa=g; } For some reason, I ...

  1. #1
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Code:
    student::student(string name,float g)
    {
    	cout <<"student constructor is running..." <<endl;
        student.sname=name; //problem line
    	gpa=g;
    }
    For some reason, I keep getting error C2143: syntax error : missing ';' before '.'

    Here's the full code:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class student
    {
    public:
        string sname;
        float gpa;
    
        student(string name);
    	student(string name,float g);
    	~student();
        void display();
    private:
    	string name;
        float g;
    };
    
    student::student(string name,float g)
    {
    	cout <<"student constructor is running..." <<endl;
        student.sname=name;
    	gpa=g;
    }
    
    student::student(string name)
    {
    	cout <<"student constructor is running..." <<endl;
        student.sname=name;
    	gpa = 0.0;
    }
    
    student::~student()
    {
    	//cout <<"Student No.: "<<i<<endl; (Is there any way to use a variable as argument here?)
    	cout <<"student destructor is running..." <<endl;
    }
    
    void student::display()
    {
    	string sname;
    	float gpa;
    
    	cout <<"Student Name:" <<sname <<endl; //sname private
    	cout <<"Student GPA:" <<gpa <<endl; //gpa private
    }
    Last edited by 843; 03-12-2011 at 09:40 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    Remove the student. from in front of the member variable names.

    And no, the destructor does NOT have parameters. It destroys exactly ONE instance at a time.
    If you've got many instances of an object, you'll get that many calls at some point.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Thanks, Salem! There is still the problem of using input parameters in destructor. Is it possible?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    See reply ^^
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,545
    Quote Originally Posted by 843 View Post
    Thanks, Salem! There is still the problem of using input parameters in destructor. Is it possible?
    No, because a destructor is called by the compiler, and its only purpose is to destroy its own instance.
    However, the destructor can naturally access all of its instances member variables. So when you create an instance, you can assign it an identifier or name. You can then print out this in the destructor.
    You could also print "this", since every instance must have a unique memory address.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what am I missing? (Program won't compile)
    By steals10304 in forum C Programming
    Replies: 3
    Last Post: 08-25-2009, 03:01 PM
  2. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21