passing objects as arquements

Hi everyone,I'm new to c++ and have a question about passing objects of classes to arquements I thought the code below would work for me but it tells me there is an error "no matching function for call to 'sec::sec(adam (&) ())"

I thought you could create a class then use it as an arquement to another classes constructor,can anyone tell me what I am doing wrong and how I can fix this problem.any help would be great

Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  #include <iostream>

using namespace std;


class adam{
  public:

  private:
};

class sec{

  public:
      sec(adam ao);
  private:

};

int main()
{

   adam addy();
   sec second(addy);
}
It's perfectly possible to do what you want to do.

The problem is in line 23:

adam addy();

That's not doing what you think it's doing. It's actually declaring a function called addy that takes no arguments, and returns an adam object. This catches a lot of people out, and is known as the "Most Vexing Parse".

To instantiate an object using the default constructor, use:

adam addy;
Last edited on
thanks for the reply Mikey,I tried to do it that way but I still get an error which reads

"undefined reference to 'sec::sec(adam

thanks
This may be a silly question, but - have you actually defined that constructor anywhere? I can see the declaration in the code you posted, but no definition of what that constructor actually does.
exactly,that was the problem I never included the curly braces that's probably why having classes in separate files is a better idea,problem solved thanks for the help =)
I'm sure you know this, but - if all you've done is add curly braces, then all you have is an empty constructor that doesn't actually do anything. Which means it's not doing anything with that adam object you're passing it.
Since the title mentions "objects as arguments" we can further note that the sec::sec(adam) takes argument by value which implies a creation of a copy. If class adam had a custom copy constructor, then it would be called by the instantiation of sec object and whatever custom side-effects would fire up. (But it doesn't in the posted code, so won't.)

Copy construction is not the only option. There is also the by reference syntax that avoids a copy. However, references allow access to a caller's variable. Const references protect the variable.

Those three have been around for a while (as is pointer, a by value argument). The new kid on the block is move. A bag of new tricks.

Anyway, there was debate before which is more efficient: a copy or a reference. The answer is always "it depends". The move cuts some corners, but adds new ones.
I still kind of have problems understanding passing by reference with objects

seems complicated coming from Java
Do you understand that, by default, when you pass an argument into a function, then what's inside the function is a copy of that argument? So that, if you change the value of the argument inside the function, it doesn't change the value of anything in the calling code?

That's what we call passing by value. The value of the thing in the calling code gets passed in, but what's inside the function is not the same object.

If you want the function to change the value of the thing that was used as the argument in the calling code, you need to pass by reference, so that what's inside the function isn't a copy, but a reference to the actual object in the calling code.
Last edited on
I understand the basics a little but for example this confuses me

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <stdexcept>

using namespace std;

int main()
{
    int a,b,c;
    a = 5;
    b = 5;

    try{

    if( b == 0)
        throw runtime_error("dive by ero error");
     c = a/b;

    }catch(runtime_error( &error)){

        cout << "exception accured" << endl;
        cout << error.what();


(this is from an example I found online)
where it says throw how come I can call or "throw" a class without making an object of that class then in the catch block I can call the class again with a memory address of an object I never made?
runtime_error("dive by ero error")

A runtime_error object was just created, and then it was thrown.
thanks for the reply Megatron =)

but how come I don't have to create it in the conventional way as in runtime_error error("divide by zero error);
That's creating an un-named, temporary object, of type std::runtime_error that then gets thrown.

You can do the same thing in other circumstances, e.g.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
  std::string A("This is A");  // Create a string object
  printString(A);  // Print out A

  // Create an un-named temporary string that gets passed to the function
  printString(std::string("This is an unnamed string"));  
  
}


void printString(std::string myString)
{
  std::cout << myString;
}

That's a very simple example, but it illustrates the point.
Last edited on
You did do it the conventional way, you just didn't
name
it. I can't tell you the proper lingo for this, but you basically just created an object without a name which was then thrown. Usually we do this if we don't need to access it by name later.

The same principle applies in these examples:

1
2
throw std::string("Some exception occured");
int a = int(10);


thanks guys,starting to make sense

but a quick question how come you would use throw could we not just use cout instead of "throwing" a message
The point of throwing an exception is to inform other parts of your code that an error has occurred, so that the error can be handled at the most appropriate place. There are plenty of tutorials about exceptions online, I'm sure.
Last edited on
yeah I was watching Buckys tutorials but a ot of them he seems to leave out a lot of explanations well detailed explainations,

I just have one final question on this topic and its where I named an object &error

how come I can pass the address of error in to the catch parameters when I never even made an object called error?
how come I can pass the address of error in to the catch parameters when I never even made an object called error?

You're misinterpreting the syntax. As MikeyBoy said, when you throw runtime_error, a temporary unnamed object of that type is created on the stack.

&error is not taking the address of some object named error. Think of the argument to a catch handler like the argument to a function.
 
  catch (runtime_error &error)

That says the catch handler is expecting one argument of type runtime_error being passed by reference. You can refer to the argument by the name error within the catch handler, just as if you had passed it to a function.

BTW, your () on line 18 around (&error) are superfluous.
Last edited on
Topic archived. No new replies allowed.