-4

I am using data.h file which have the following code

#ifndef __DATA_h_INCLUDED__
#define __DATA_h_INCLUDED__

#include "string"

struct data {
    std::string location="";
    int year = 0, month = 0;

    data();
    data(std::string location, int year, int month);
};

#endif

and the data.cpp file looks like this

#include "data.h"
#include "string"

using namespace std;

data::data() {
    //initialize the data members (location,year,month)
} 

data::data(std::string loc, int year, int month) {
    //initialize the data members (location,year,month)
}

in some other .cpp file how can i get these values and initialize these values.

node.h

struct Node {
data d;

Node(std::string id, int year, int month); 

};

node.cpp

Node::Node(string id, int year, int month){
// here i want to initialize 'data' 

}

print.cpp

Node* node;
cout<<node->data->location;
Awais Saleem
  • 51
  • 1
  • 12

4 Answers4

3

They are already initialized for the default coinstructor (which shoudl proably be =default instead).

Then just use the initialization list:

data::data(std::string loc, int year, int month):loc(std::move(loc)), year(year), month(month) {
}

Include string properly as well:

#include <string>
Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
1

in your "data.cpp", you can initialize the members like this:

#include "data.h"
#include "string"
using namespace std;
data::data() : year(0), month(0) {
    //initialize the data members (location,year,month)
    //in fact, 'location' donot need initialization, 
    //because the member will be constructed first as 
    //a empty string before give control to user-defined constructor.
    location = "";
} 
data::data(std::string loc, int _year, int _month)
    year(_year), month(_month) {
    //initialize the data members (location,year,month)
    location = loc; // or location.assign(loc);
}

when you use the structure in other cpp file, you may use is like this:

#include "data.h"
data x; //call default constructor: data();
//since struct 's member is implicitly public, 
//you can access them from outside of its defination.
x.location = "your location";
x.location.assign("some other place");
x.location.append("etc");
x.year = 2018;
x.month = 11;
Exlife
  • 99
  • 6
0

Initializing data in constructor is done like this:

data::data() : 
    location(""), year(0), month(0)
{
} 

data::data(std::string loc, int year, int month) : 
    location(loc), year(year), month(month)
{
}

In some other cpp file, for example, main.cpp, you can use this like that:

#include <iostream>    
#include "data.h"

int main()
{    
// initializing
    data obj("NY", 2018, 11);

// using  
    std::cout << "Year:  " << obj.year << std::endl;
    std::cout << "Month: " << obj.month << std::endl;
    std::cout << "Loc:   " << obj.location << std::endl;

// setting properties
    obj.year = 2100;
    obj.month = 1;
    std::cout << "Year:  " << obj.year << std::endl;
    std::cout << "Month: " << obj.month << std::endl;

// initializing by default values
    data obj2();
}
snake_style
  • 1,139
  • 7
  • 16
  • thanks for your help can you please clarify that how can i use in data.cpp file at the time of initialization – Awais Saleem Nov 14 '18 at 11:43
  • Sorry, what do you mean "use data.cpp at time of initialization"? – snake_style Nov 14 '18 at 11:47
  • for example in temp.h `data d; Node(std::string id, int year, int month); and in temp.cpp Node::Node(std::string id, int year, int month) and in print.cpp I want to get a location Node * n; cout<data->location;` – Awais Saleem Nov 14 '18 at 12:59
  • it says "data" has no member "location" I can share print.cpp and temp.h and temp.cpp – Awais Saleem Nov 14 '18 at 13:05
0

Don't overcomplicate this. If you start coding, don't split your code up in too many files.

Other than that, learn about struct member initialization. Either on this site or at your favorite documentation page.

For member access you use the arrow-operator when you have a pointer to an object, vs. the dot-operator that you use when you have the object directly. For further reading:

Here's example code:

#include <iostream>

struct data {
    int x_;
    // use member initialization list to init the data value directly
    data(int x) : x_(x) {}
};

struct node {
    data data_;
    // use member initialization list to init the data value directly
    node(int x) : data_(x) {}
};

int main() {
    // create object
    node n(42);
    // acquire pointer to object
    node *p = &n;
    // use arrow to access member with pointer, use dot to access with object
    std::cout << p->data_.x_ << '\n';
}

The output is:

$ g++ test.cc && ./a.out
42

And because it seems you might be into implementing some sort of data structure, you might also want to learn about object lifetime and ownership issues with manual memory management. So some references for further education:

moooeeeep
  • 31,622
  • 22
  • 98
  • 187