Link to home
Start Free TrialLog in
Avatar of garavindbabu
garavindbabu

asked on

cpp programming in passing objects as references

hello everyboby!

i am doing a project in which i am using 2 functions namely

1.sq::sq(std::vector<hvector>& points, sq &init);

 i am using this function  for constructing a 3D image and fitting some 3d points in it.so the arguments goes like this

points are the 3D points and hvector is a class defined as

     class hvector {

  public:
  double x;
  double y;
  double z;


so i called the above function as

sq s2=sq(&points,&s1);


where points are of type hvector and s1 is an object of sq.

but i am getting the error as :

main1.cpp:134: no matching function for call to `sq::sq (vector<hvector,allocator<hvector> > *, sq *)'



the other function i am using is

void sq::plotSQsPlusPoints(ofstream& out,  const std::vector<hvector>& vPoints );

this is used for diaplaying the sq and the points.

so i am  calling the above function as


s2.plotSQspluspoints(out,&points);

where out ias an ofstream and points are the 3D points.

but i am getting the error as


main1.cpp:140: no matching function for call to `sq::plotSQspluspoints (ofstream &, vector<hvector,allocator<hvector> > *)'


i will be thankful and convey my regards if i could get a solution.

with regards
aravind
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Salte
Salte

Your problem is that you define the parameter as reference and then you pass it a pointer.

Pointers and references are different types in C++. This can be confusing - in particular because people often use the term 'reference' when they refer to a pointer and not a reference. In C there are no reference types and so people often do that, in C++ where reference and pointers are two different types it is a good habit of always have distinction between the two - especially if it is not clear which one you mean and where it makes a difference.

Your proper call to the construct is something like:

sq s2(points,s1);

"points" is also a reference parameter and not a pointer so that too should also be passed as a reference argument and not as a pointer argument.

However, if this is the only constructor you have for sq you're in trouble. Since you always need a reference to some other object you get the problem of how to construct your very first object. The one that doesn't have any reference to any other object.

For such situations you can do one of the following:

make an overload constructor that doesn't take the extra argument:

sq::sq(std::vector<hvector> & points)
{ .... }

Then you're ready.

If you only have the constructor that require another object you run into problems how you can construct the initial object. Since every object require you have an object of that type already.... I hope you see the problem.

Alf
Avatar of garavindbabu

ASKER

thanx for u r reply i got it now.but i could not solve the 2 function. i called it as u said but again getting the same error.may be the ofstream cannot be passed as reference.can u say something
>> the ofstream cannot be passed as reference.

It should get passed by reference. Please post a part of your code (where you've declared, defined and called the function).

I'm leaving for the day but will get back to you tomorrow. Please keep this page updated.

Mayank.
thanx mayankeagle i could solve the problem. thanx to  all who gave me suggestions