Thread: no default constractor ....

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    44

    no default constractor ....

    Code:
    #include "stdafx.h"
    #include<string>
    using namespace std;
    
    struct student1{
    const string name;
    const double salary;
    const string student1::getName();
    const double student1::getSalary();
    };
    
    
    #include "stdafx.h"
    #include<iostream>
    #include<string>
    #include"student1.h"
    using namespace std;
    
    const string name = "Mike";
    const double salary = 123.3;
    
    const string student1::getName()
    {
           return name;
    }
    
    const double student1::getSalary()
    {
           return salary;
    }
    
    
    
    #include "stdafx.h"
    #include"student1.h"
    #include<string>
    #include<iostream>
    using namespace std;
    
    int main()
    {
    	student1 me;
        cout << me.getName();    
        cout << me.getSalary();
    	return 0;
    }
    
     error C2512: 'student1' : no appropriate default constructor available
    Whats the problem here??? and why do I have to have a constractor when using a struct...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The name and salary member variables are const, so if you do not initialise them in a constructor, they will always only contain garbage.

    I guess you think that:
    Code:
    const string name = "Mike";
    const double salary = 123.3;
    initialises them, but actually, you just create two global constants that have nothing to do with student1.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    44
    So I MUST have a constractor right???

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, or you can make those member variables non-const.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by mixalissen View Post
    So I MUST have a constractor right???
    More specifically, a default constructor: one which can be invoked with no arguments.

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    And you must also initialise the variables in the struct's member initialisation list as I don't believe you can do it in-function if it's a purely a const data member. You should probably make it a class really. It's just my style but I only use structs for data storing. Soon as functions are introduced I call it a class.

  7. #7
    Banned
    Join Date
    May 2008
    Location
    Four Dots Planet
    Posts
    72
    Quote Originally Posted by twomers View Post
    And you must also initialise the variables in the struct's member initialisation list as I don't believe you can do it in-function if it's a purely a const data member. You should probably make it a class really. It's just my style but I only use structs for data storing. Soon as functions are introduced I call it a class.
    yep const members and references are to be in initialization list only and by the way just putting a constructor and destructor in a struct does not make it move away from its pod definition in any way its okay to have (con/de)structors in a struct without having them make a class its not the point of choice either since they are still behaving like a struct whereas when you think about class they mean a hell lot more and that's why we have struct and class both keywords in c++

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by (::) View Post
    yep const members and references are to be in initialization list only and by the way just putting a constructor and destructor in a struct does not make it move away from its pod definition in any way its okay to have (con/de)structors in a struct without having them make a class its not the point of choice either since they are still behaving like a struct whereas when you think about class they mean a hell lot more and that's why we have struct and class both keywords in c++
    Rubbish. The only difference between a class and a struct in C++ is the default access (class members and bases are private by default, and struct members and bases are public).

    A class or struct with a user-defined destructor is also, by definition, not a POD-type. From the C++ standard; "A POD-struct is an aggregate class that has no non-static data members of type pointer to member, non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-defined copy assignment operator and no user-defined destructor. Similarly, a POD-union is an aggregate union that has no non-static data members of type pointer to member, non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-defined copy assignment operator and no user-defined destructor. A POD class is a class that is either a POD-struct or a POD-union."

  9. #9
    Banned
    Join Date
    May 2008
    Location
    Four Dots Planet
    Posts
    72
    i am not saying class and struct are any more different than they actually are. one thing. other thing is just adding (con/de)structors to them does not mean that turn them into a class. other thing.

    period.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    other thing is just adding (con/de)structors to them does not mean that turn them into a class.
    I am not sure what you mean by "class", but grumpy quoted the C++ Standard to you. Your statement that a struct with a user defined destructor is a POD struct is simply incorrect.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Banned
    Join Date
    May 2008
    Location
    Four Dots Planet
    Posts
    72
    Quote Originally Posted by laserlight View Post
    I am not sure what you mean by "class", but grumpy quoted the C++ Standard to you. Your statement that a struct with a user defined destructor is a POD struct is simply incorrect.
    i was using pod in a liberal sense and not literal sense with (con/de)structors we just have convenient ways of initializing and cleanup without having to add extra methods to struct and having the user to call them at right times

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Sure, we can think of a struct as a POD. It's certainly what I do, but in the eyes of the standard, a struct isn't a POD, so one must be aware of this because some utilize the fact that struct == class (though visibility is, by default, not the same).
    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. member as default argument
    By MarkZWEERS in forum C++ Programming
    Replies: 2
    Last Post: 03-23-2009, 08:09 AM
  2. Utilizing another compiled program for a task.
    By kotoroshinoto in forum C Programming
    Replies: 6
    Last Post: 06-03-2008, 01:43 PM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM
  5. Switching Default Buttons :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 07-02-2002, 04:08 PM