0

I'm currently running into a few problems surrounding passing an objects return value through a function. My function is prototyped in .h and defined in .cpp. the function for example Class::function(int value); takes in an integer of value. When I'm calling the object objectName name; and passing it through as Class::function(name);.

// foo.h
class foo
{
     void function(int value);
}


//foo.cpp
void foo::function(int value)
{
     // CODE HERE
}


//main.cpp
int main()
{
     objectName name;
     foo::function(name);
}

The errors that I am receiving go along the lines of:

Main.cpp:43:23: error: no matching function for call to ‘class::function(objectName&)’ name.function(name); // passes value to the function

3 Answers3

2

The call foo::function(name); is triple wrong.

  1. It's a call to a static function, but function is not static, so you need a instance of foo to call the function:
foo myFoo;
myFoo.function(0);
  1. function is private, so it can not be used, outside of foo. All class members are by default private, so you need to declare it as public:
class foo {
  public:
    void function(int);
}
  1. function takes an int as parameter, but you pass some object of type objectName. That's more a design issue, what parameter should function take? int or objectName? Maybe you want
//header
class foo {
  public:
    void function(objectName &);
}

// cpp
void foo::function(objectName &value) {
}
Lukas-T
  • 11,133
  • 3
  • 20
  • 30
0

The declaration and definition of foo is correct. However, that is not how you create and use objects in C++.

int main() {
  foo foo_obj; // Create an object of type 'foo' named 'foo_obj'
  foo_obj.function(1); // call function named 'function' on the object 'foo_obj' 
                       // and pass integer (=1) as an argument

  return 0;
}

You should definitely pick up a good C++ book

Mike van Dyke
  • 2,724
  • 3
  • 16
  • 31
0

I see an allocated variable named name, and it has a type objectName.

I see no allocation of any other objects, especially of type foo.

You can't call a class method, like foo::function() without an object of type foo. (except static declarations, which you are not using and do not need)

Mainly, that error you see is because you only have a class method that takes an int parameter, and no function that takes a parameter of type objectName.

donjuedo
  • 2,475
  • 18
  • 28