object as parameter error

Hi guys I thought I did everything right here when passing an object as parameters but for some reason I'm getting an error on line 21 saying 'passObject' was not declared in this scope,anyone know where I went wrong?

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
26
27
28
29
30
31
  
#include <iostream>

using namespace std;


void passObject(one oo);

class one{

   public:
       void sayHi(){

          cout << "say Hi" << endl;

       }
};

int main()
{
    one adam;
    adam.sayHi();
    passObject(adam);
}


void passObject(one oo){

   oo.sayHi();

}
Last edited on
The compiler starts at the top and goes down. When it gets to line 20, it has never heard of the function passObject. You need to have the function definition or declaration before you try to use the function.
I prototyped the function I corrected that but I still get an error

the error now says line 5 'one' was not declared in this scope
Last edited on
The error is on line 7: 'one' was not declared.
Declare class one before declaring the function that has a parameter of type one

1
2
3
4
5
6
class one ; // *** added *** declare class one
void passObject(one oo);

class one{
  // ...
};
Thanks JL,thats really new to me and kind of shocked me because I did not know classes had to be declared,all this time I've been using c++ and I thought functions where the only things that had to be declared,I'm pretty sure I've got away with it in the past that's why it confuses me so much
> I did not know classes had to be declared,all this time I've been using c++ and I thought
> functions where the only things that had to be declared

Any name has to be declared before it is used.


> I'm pretty sure I've got away with it in the past that's why it confuses me so much

You might have used an elaborated-type-specifier
http://en.cppreference.com/w/cpp/language/elaborated_type_specifier
A class name can be implicitly declared by an elaborated-type-specifier

For instance:
1
2
3
4
5
6
// class one ; // not declared
void passObject( class one oo ); // elaborated_type_specifier implicitly declares class one

class one{
  // ...
};


Thanks for clearing up that problem =)
Topic archived. No new replies allowed.