How to pass an object as a function parameter of another class

hi,
I want to pass an object as a parameter to a function, the object belongs to another class and locates in MobileNode class(MobileNode.h), and the function is in mac_tdma.cc.
T do this in mac_tdma.cc:
void MacTdma::NodeLocation(MobileNode *node)

and Called it like this:
NodeLocation(node);

in mobilenode.h, I creat an object of mobilenode:
MobileNode *node;
but I encounter this error:
error: ‘node’ was not declared in this scope
Last edited on
1) Please use code tags when posting code, to make it readable

2) You haven't shown us nearly enough code for us to be able to tell what's wrong. There doesn't seem to be anything glaringly wrong with what you've shown us, so the problem must be in code you haven't shown us.

Please post a minimal, compiling code example that demonstrates the problem you're seeing.
Overall declarations, definitions, and implementations should be kept separate. Include only what you need.

Foo.h
1
2
3
4
5
6
7
8
9
10
11
#ifndef FOO_H
#define FOO_H

class Bar; // forward declaration of a typename

class Foo // define a class
{
public:
  void fubar( Bar* ); // declare member
};
#endif // FOO_H 


Bar.h
1
2
3
4
5
6
7
8
9
#ifndef BAR_H
#define BAR_H

class Bar // define a class
{
public:
  void oink();
};
#endif // BAR_H 


driver.cpp
1
2
3
4
5
6
7
8
#include "Foo.h"
class Bar;

int main() {
  Bar* bar {};
  Foo foo;
  foo.fubar( bar );
}


Foo.cpp
1
2
3
4
5
6
7
#include "Foo.h"
#include "Bar.h"

void Foo::fubar( Bar* gaz )
{
  if ( gaz ) gaz->oink();
}


Bar.cpp
1
2
3
4
5
#include "Bar.h"

void Bar::oink()
{
}

Last edited on
Topic archived. No new replies allowed.