cpp programming in passing objects as references

Posted on 2003-03-17
Medium Priority
Last Modified: 2010-04-02
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
Question by:garavindbabu
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 30

Accepted Solution

Mayank S earned 150 total points
ID: 8151067
>> 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 ) ;


LVL 12

Expert Comment

ID: 8151261
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.


Author Comment

ID: 8151913
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
LVL 30

Expert Comment

by:Mayank S
ID: 8152094
>> 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.


Author Comment

ID: 8155691
thanx mayankeagle i could solve the problem. thanx to  all who gave me suggestions

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question