passing class pointers in a class method

cplusplus forum,
I need to pass a pointer to One class in a function of Another class.

ex:
class One
{
public:
};

class Another
{
public:
void function(One* one)
{ do stuff; };
};

main()
{
One one;
Another another;
}

The compiler error is syntax error: identifier 'One'

What am I missing here?

jerry


Seems OK.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class One
{
  public:
};

class Another
{
  public:
    void function(One* one)
    { };
};

int main()
{
  One one;
  Another another;
  another.function(&one);
}

Salam,
Yes it seems OK but the compiler doesn't like it.

If I make the function global it compiles without error.

jerryd
Yes it seems OK but the compiler doesn't like it.
Post the exact source code that exhibits the problem. Psuedocode is not helpful.

Obviously we cannot identify specific syntax problems in a program we can't see.
Last edited on
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
#include <iostream>

class One
{
public:
    int data;
    One(){data = 99;};
};

class Another
{
public:
    Another(){};
    int function(One* );
};

int Another::function(One* one) // <-- possibly Another:: missing?
{
    return one -> data;
}

int main()
{
    One one;
    Another another;
    
    std::cout << another.function(&one) << '\n';
}


99
Program ended with exit code: 0


Topic archived. No new replies allowed.