Initializing a nested structure throws error in C++ - c++

I am learning structures in C++. Got the following error while executing the code:
Error: Too many initializer for CompData.
It would be great if someone could point out the mistake.
Code:
#include <iostream>
#include <string>
struct EmpData
{
std::string name;
int age;
double salary;
};
struct DepartmentData
{
std::string departmentName;
struct EmpData;
};
struct CompData
{
std::string compName;
struct DepartmentData;
};
int main()
{
CompData DeltaF
{
"DeltaF",
{
"Product Development",
{
"Steve", 35, 1223
}
}
};
}

Here:
struct DepartmentData
{
std::string departmentName;
struct EmpData;
};
You declare a structure EmpData, full name is DepartmentData::EmpData. It is a different type than EmpData you declared and defined above. If you want DepartmentData to have a member of type EmpData you can remove struct and need to give it a name:
struct DepartmentData
{
std::string departmentName;
EmpData empData; // member of type EmpData called empData
};
Same for the DepartmentData member of CompData. If you fix those, your code compiles without errors.

Related

How to access a private structure within a class in C++?

Good day everyone! I would just like to ask if how it is possible to access a private structure inside a class?
My code looks like this:
class VideoClass{
private:
struct vidstruct {
int Video_ID;
string movietitle;
string genre;
string prod;
int numberOfCopies;
string MovImg_name;
};
public:
VideoClass();
// ~VideoClass();
void insertVideo(vidstruct info);
void rentVideo(int rv); //
void returnVideo(); // ---
void showDetails(int sd);
void validateVideo(); //
void displayVideo();
};
What I wanted to is to access the vidstruct (name of the structure) to any parts of my program. Thankyou for your kind answers in advance :)
The fact that your member vidstruct is declared as private means that it is not accessible outside of the class. If it is meant to be used outside of the class You could declare it simply outside of it.
Here would be a snippet example (printing 'movie test'):
#include <iostream>
using namespace std;
typedef struct {
int Video_ID;
string movietitle;
string genre;
string prod;
int numberOfCopies;
string MovImg_name;
} vidstruct;
class VideoClass{
public:
VideoClass() {
};
// ~VideoClass();
void insertVideo(vidstruct info) {
cout << info.movietitle;
};
void rentVideo(int rv); //
void returnVideo(); // ---
void showDetails(int sd);
void validateVideo(); //
void displayVideo();
};
int main()
{
vidstruct x;
x.movietitle = "movie test";
VideoClass videoTest;
videoTest.insertVideo(x);
return 0;
}

How to pass Dynamic array of structures in c++?

I have created an array dynamically of structures and now i am willing to pass it to function.What is the correct method of doing it?What should i put in parameter of function in MAIN for doing it?
void function(Data *family)
{
//code
}
int main()
{
struct Data{
string name;
int age;
string dob;
};
Data *family = new Data[3];
function(Data); //ERROR in parameter i guess!
}
It is better to use more safe ways using std::vector or std::shared_ptr. Because it is easy to make a mistake when you use raw pointers.
If you really need to use raw pointer than you need fix your code:
#include <string>
#include <iostream>
// "Data" should be declared before declaration of "function" where "Data" is used as parameter
struct Data {
std::string name;
int age;
std::string dob;
};
void function(Data *family)
{
std::cout << "function called\n";
}
int main()
{
Data *family = new Data[3];
// fill all variables of array by valid data
function(family); // Pass variable "family" but not a type "Data"
delete[] family; // DON'T FORGET TO FREE RESOURCES
return 0; // Return a code to operating system according to program state
}
Every c++ programmer needs to learn std::vector, which is a dynamic array:
#include <vector>
struct Data{
string name;
int age;
string dob;
};
void function(const std::vector<Data>& family)
{
//code
}
int main()
{
auto family = std::vector<Data>(3);//family now contains 3 default constructed Data
function(family);
}
Not sure what actually what actually you are looking for, I guess you can try like this:
First define your structure outside from main so it would be accessible as function parameter. Then instead of Data pass object family to the function.
struct Data {
string name;
int age;
string dob;
};
void function(Data *family)
{
//code
}
int main()
{
Data *family = new Data[3];
function(family);
}

C++ struct in a vector in an object(class)

was in this for few hours now and I cannot seem to find the right way to do this. I am creating a vector that will be populated with structs but I could not make it to work. I have tried making a struct and put the struct from the object in there but received an error. Anyways this is my work, I am really new into this so I hope you guys could help me out.
main.cpp
#include <iostream>
#include "CBank.h"
int main()
{
CBank bank;
struct account = bank.account;
bank.account.name = "Alpha Omega";
bank.account.money = 15635.23;
bank.account.pin = 3241;
bank.add.push_back(account);
return 0;
}
CBank.h
#ifndef CBANK_H
#define CBANK_H
#include <iostream>
#include <vector>
class CBank
{
public:
CBank();
private:
struct account { std::string name;
float money;
short pin;
};
std::vector<account> add;
};
#endif // CBANK_H
CBank.cpp
#include "CBank.h"
CBank::CBank()
{
//ctor
}
The problem is that you define a type (account) in the class. account is a type so you should not declare it in the class :
struct account { std::string name;
float money;
short pin;
};
and then, the class becomes :
class CBank
{
public:
CBank();
account acc;
std::vector<account> add;
};
and the main :
int main()
{
CBank bank;
bank.acc.name = "Alpha Omega";
bank.acc.money = 15635.23;
bank.acc.pin = 3241;
bank.add.push_back(bank.acc);
return 0;
}
Your struct account and add vector are private members of CBank. To access them you need to make them public like your class constructor is.

Use a struct in another struct

Hi there I keep getting error for the code below which I think makes sense!
I'm trying to use a member of the first struct in a function in the second struct. I code in Xcode and this is the error I get:
No member named 'PLZ' in 'std::__1::vector >'
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct place {
string PLZ;
string name;
};
struct adresse {
string firstN;
string lastN;
string Str;
string Hsnum;
string PLZ;
void print(vector<place> a){
cout<<a.PLZ;
}
};
int main()
{
return 0;
}
Just define the function at least like
void print(const vector<place> &a) const
{
for ( const auto &place : a )
{
cout << place.PLZ << ' ';
}
}
std::vector is a container that can contain several elements. So you have to specify whether you are going to output all elements of a vector or a concrete element.
The function above outputs the data member PLZ of each element of the vector.

Struct Members initialization

How do I initialize the member variables in the following code?
#include <string>
#include <iostream>
using namespace std;
int main()
{
typedef struct Employee{
char firstName[56];
char lastName[56];
};
typedef struct Company {
int id;
char title[256];
char summary[2048];
int numberOfEmployees;
Employee *employees;
};
typedef struct Companies{
Company *arr;
int numberOfCompanies;
};
}
You can add a constructor like this one:
struct Employee{
char firstName[56];
char lastName[56];
Employee() //name the function as the struct name, no return value.
{
firstName[0] = '\0';
lastName[0] = '\0';
}
};
As you are already use std::string as indicated from the #include statement, you should change your classes declarations as follows (and do so outside of main() functions body):
struct Employee {
std::string firstName;
std::string lastName;
};
typedef struct Company {
int id;
std::string title;
std::string summary;
int numberOfEmployees;
Employee *employees;
};
typedef struct Companies{
Company *arr;
int numberOfCompanies;
};
first: u use typedef in a wrong way
typedef struct Employee{
char firstName[56];
char lastName[56];
};
you need the other name for typedef
typedef struct _Employee{
char firstName[56];
char lastName[56];
}Employee;
but since u're using c++ typedef is not needed.
Second: don't declare structs inside the function. Declare them outside of main.
Third: use constructors to initialize members to default value, example:
struct Employee{
Employee()
{
strcpy (firstName, ""); //empty string
strcpy (lastName, "asdasd"); // some value, every object now by default has this value
}
char firstName[56];
char lastName[56];
};
fourth: use std::string class (#include) for easier string handling
fifth: consider to type class instead struct, the only difference between those two, is that class declares the variables as private by default(you can change visibility of variable urself), while struct declares them as public by default.