473,398 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Initialising struct in C++

Natasha26
I was playing around with struct and realised that when i inherit from a base-class, i can no longer initialise the inherited struct the way i'm used to.

Expand|Select|Wrap|Line Numbers
  1. struct Date{
    int dd, mm, yy;
    }
  2.  
  3. struct Employee{
    char* fname, *lname;
  4. Date hiring_d;
  5. short dept;
  6. };
  7.  
  8. struct Manager : public Employee{
    short level; //i've tried commenting this line for furher testing but no luck
    };
  9.  
Here's how i initialise:
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. Employee e1 = {"Snoop", "Dogg", ' ', {25,3,2008}, 101}; //this is ok
  4. Manager m1 = {"Gin", "Juice", '& ', {01,01,2000}, 101, 2}; //this fails
  5. }
  6.  
I use Borland free compiler 5.5. And the error i get is:
Error E2190 Ch12DerivedClass.cpp 43: Unexpected }
Any idea? It's probably not possible to initialise derived structures like that? Nat.
Mar 25 '08 #1
5 2906
gpraghuram
1,275 Expert 1GB
I think there is a missing semicolon in the structure declaration.

Expand|Select|Wrap|Line Numbers
  1. struct Date{
    int dd, mm, yy;
    };//Semicolon was missing here
  2.  
  3. struct Employee{
    char* fname, *lname;
  4. Date hiring_d;
  5. short dept;
  6. };
  7.  
  8.  

Raghuram
Mar 25 '08 #2
I think there is a missing semicolon in the structure declaration.

Expand|Select|Wrap|Line Numbers
  1. struct Date{
    int dd, mm, yy;
    };//Semicolon was missing here
  2.  
  3. struct Employee{
    char* fname, *lname;
  4. Date hiring_d;
  5. short dept;
};
  •  
  •  

  • Raghuram I wrongly copied & pasted my code. Sorry about that, but that's not the problem. I can't initialise the inherited structure using ={.,.,..};
    Mar 25 '08 #3
    gpraghuram
    1,275 Expert 1GB
    I wrongly copied & pasted my code. Sorry about that, but that's not the problem. I can't initialise the inherited structure using ={.,.,..};


    the following declaration works after some modification
    Expand|Select|Wrap|Line Numbers
    1. Employee e1 = {"Snoop", "Dogg", {25,3,2008}, 101}; //Removed the ' '
    2.  
    But you cant initialize the manager object as it is inheriting the other structure and you shold have a constructor to do this.
    Other members please comment on this

    Raghuram
    Mar 25 '08 #4
    weaknessforcats
    9,208 Expert Mod 8TB
    To Natasha26: classes are implemented as structs in C++. The only difference is the default access to member of a class is private whereas for a struct it is public.

    That is, if you specify private/public/protected for every member of your struct it is identical to a class.

    That means, you use construtors, destructors, etc with your structs.

    That means, you initialize your base struct by using an initialization list in the constructor of the derived struct.
    Mar 25 '08 #5
    To Natasha26: classes are implemented as structs in C++.

    //....

    That means, you initialize your base struct by using an initialization list in the constructor of the derived struct.
    Well spotted weaknessforcats! Also thank you Raghuram. Cheers guys. T.
    Mar 25 '08 #6

    Sign in to post your reply or Sign up for a free account.

    Similar topics

    3
    by: Tim Clacy | last post by:
    Can this be done... or is there a better way to achieve the same objective? If an interface class contains only references (to yet more interface classes), then can those references be...
    8
    by: Nick Keighley | last post by:
    Hi, is there a way of initialising a vector in a similar fashion to initialising an array? consider: Shape* triangle = new Triangle (); Shape* square = new Square (); Shape* shape_arr =...
    7
    by: Nidhi | last post by:
    Hi all, I dont know whether this question has been raised before or not. Nevertheless, here is my question... I have a structure of around 255 elements of the same type, say int. I have an...
    2
    by: shan_rish | last post by:
    Hi CLCers, In the below program the error message while compiling is /home1/murugan/prog/cprog >cc -o struct_eval struct_eval.c cc: "struct_eval.c", line 14: error 1549: Modifiable lvalue...
    107
    by: DaveC | last post by:
    I always used to initialise variables at declaration, then a couple of colleagues started telling me it was bad practice and that the compiler should be left to spot the use of uninitilised...
    4
    by: Jay | last post by:
    In C++ I can initialise a struct as follows: struct fileInfo {char filename; int gotFile; int ch; int ln;}; fileInfo configFile = {"", 0, 1, 1}; Is there a similar way to initialise a struct in...
    4
    by: Nick Keighley | last post by:
    Hi, is this correct:- typedef struct { long frequency; } Structure; Structure my_struct = {0};
    0
    by: James Kanze | last post by:
    On Jun 27, 3:06 pm, Angus <anguscom...@gmail.comwrote: You can initialize it with an array. You can't use agglomerate initialization on it, however, because it isn't an agglomerate. The...
    33
    by: Adam Chapman | last post by:
    Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d...
    0
    by: Charles Arthur | last post by:
    How do i turn on java script on a villaon, callus and itel keypad mobile phone
    0
    by: emmanuelkatto | last post by:
    Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
    0
    BarryA
    by: BarryA | last post by:
    What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
    1
    by: Sonnysonu | last post by:
    This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
    0
    by: Hystou | last post by:
    There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
    0
    Oralloy
    by: Oralloy | last post by:
    Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
    0
    by: Hystou | last post by:
    Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
    0
    agi2029
    by: agi2029 | last post by:
    Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
    0
    isladogs
    by: isladogs | last post by:
    The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

    By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

    To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.