I've read of similar problems to this, but the solutions provided didn't work for me so.
I want to call a function that exists in another class located in a different .cpp file. I don't want to create an instance of the object, I just want to use the function.
My code that tries to call the function:
switch (option)
{
case 1:
cout << "\nDoing stuff\n\n" ;
Controller::AlbumOps SayHey();
//SayHey should have run but isn't working
break;
And the function I'm trying to call:
#include "Menu.hpp"
#include "Album.hpp"
#include "stdio.h"
#include "AlbumOps.hpp"
#include <iostream>
using namespace std;
namespace Controller
{
static void Controller::AlbumOps::SayHey ()
{
cout << "Hey\n";
}
}
When I execute the code, the Hey is never printed. I thought the solution was to make the function static but that hasn't worked for me.
The call should be
Controller::AlbumOps::SayHey(); // ^^ // double-colon
You should put static
on the in-class function declaration , not the out-of-class function definition (where it means something completely different, "internal linkage"). That is:
in the header ( AlbumOps.hpp ):
// ... namespace Controller { class AlbumOps { public: // ... static void SayHey(); // Note: 'static' here }; } // ...
and in the implementation file ( AlbumOps.cpp ): either:
// ... void Controller::AlbumOps::SayHey() // Note: no 'static' { cout << "Hey\\n"; } // ...
or:
// ... namespace Controller { // ... void AlbumOps::SayHey() // Note: no 'static', no repeated 'Controller::' { cout << "Hey\\n"; } // ... } // ...
(For the record, what you current
Controller::AlbumOps SayHey();
// ^
// space
does is locally declare a function named SayHey
taking no parameter and returning a Controller::AlbumOps
(search for "C++ most vexing parse").)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.