Question

I have a class called locationdata which has a friend class called PointTwoD

#include <string>
#include <iostream>

using namespace std;

class locationdata
{
  public:
  locationdata(); //default constructor
  locationdata(string,int,int,float,float); //constructor

 //setter
 void set_sunType(string);
 void set_noOfEarthLikePlanets(int);
 void set_noOfEarthLikeMoons(int);
 void set_aveParticulateDensity(float);
 void set_avePlasmaDensity(float);

 //getter 
 string get_sunType();
 int get_noOfEarthLikePlanets();
 int get_noOfEarthLikeMoons();
 float get_aveParticulateDensity();
 float get_avePlasmaDensity();


 float computeCivIndex();
 friend class PointTwoD;  //friend class

  private:

  string sunType;
  int noOfEarthLikePlanets;
  int noOfEarthLikeMoons;
  float aveParticulateDensity;
  float avePlasmaDensity;

};

I have another class called PointTwoD which is suppose to contain the class: locationdata as a private member .

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

using namespace std;

class PointTwoD
{
  public:
  PointTwoD();
  locationdata location; // class


  private:
  int x;
  int y;

  float civIndex;

};

When i try to instantiate a PointTwoD object in my main() , and use the functions fromn locationdata , i get a error : request for member 'location' in "test" which is of non class type PointTwoD()().

#include <iostream>
#include "PointTwoD.h"
using namespace std;

int main()
{
     int choice;

   PointTwoD test();

    cout<<test.location->get_sunType; //this causes the error
}

My questions is

1) Why doesnt my friend class work , i thought i should be able to access all properties use all functions from friend once it has been declared

2) Should i use inheritance instead of friend class to access all the method and properties of class locationdata from class PointTwoD ??

First UPDATE : After i changed declaration from PointTwoD test() to PointTwoD test , i get the following error : base operand of '->' has non-pointer type , what does it mean and how to solve it

Was it helpful?

Solution

This here:

PointTwoD test();

is a function declaration, not a variable definition.

You need:

PointTwoD test;

or in C++11:

PointTwoD test{};

See http://en.wikipedia.org/wiki/Most_vexing_parse for more info.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top