• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

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 {

  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


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
  • 2
  • 2
1 Solution
Mayank SAssociate Director - Product EngineeringCommented:
>> sq s2=sq(&points,&s1);

Change it to:

sq s2 = sq ( points, s1 ) ;

Ampersand (&) is used for passing addresses, i.e., when the argument is a pointer like sq *, not sq & - that is a reference variable.


>> s2.plotSQspluspoints(out,&points);

Change it to:

s2.plotSQspluspoints ( out, points ) ;


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.

garavindbabuAuthor Commented:
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
Mayank SAssociate Director - Product EngineeringCommented:
>> 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.

garavindbabuAuthor Commented:
thanx mayankeagle i could solve the problem. thanx to  all who gave me suggestions

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now