no default constractor ....

This is a discussion on no default constractor .... within the C++ Programming forums, part of the General Programming Boards category; Code: #include "stdafx.h" #include<string> using namespace std; struct student1{ const string name; const double salary; const string student1::getName(); const double ...

  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
    21,717
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,717
    Yes, or you can make those member variables non-const.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,289
    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,262
    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,289
    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
    21,717
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,596
    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, 04: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

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