So the title is somewhat misleading. But its exactly what I am trying to do. I created a small case scenario. This case works in Visual Studio but when trying it on Mingw I get an error. Here is the case. I am trying to call a function inside a cpp file from a static method which resides in a different cpp file. This is just rough code which will get my point across.
File:foo.h
#ifndef FOO_H_INCLUDED
#define FOO_H_INCLUDED
#include <iostream>
struct foo
{
int someMethod();
};
#endif // FOO_H_INCLUDED
File: foo.cpp
#include "foo.h"
int someFunction()
{
std::cout << "SomeFunction";
return 0;
}
int foo::someMethod()
{
std::cout << "foo called";
return 0;
}
File:main.cpp
void myfunction()
{
}
struct bar
{
static void somebar()
{
someFunction(); //error: 'someFunction' was not declared in this scope
myfunction(); //OK
}
};
int main()
{
}
My question is why am I getting an error on someFunction(); This is my compiler output
g++.exe -Wall -std=c++98 -g -std=c++11 -I..\..\..\mingw64\include -c C:\Users\peeru\TestCodeBlocks\foo.cpp -o obj\Debug\foo.o
C:\Users\peeru\TestCodeBlocks\foo.cpp: In function 'int someFunction()':
C:\Users\peeru\TestCodeBlocks\foo.cpp:6:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
C:\Users\peeru\TestCodeBlocks\foo.cpp: In member function 'int foo::someMethod()':
C:\Users\peeru\TestCodeBlocks\foo.cpp:11:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
g++.exe -Wall -std=c++98 -g -std=c++11 -I..\..\..\mingw64\include -c C:\Users\peeru\TestCodeBlocks\main.cpp -o obj\Debug\main.o
C:\Users\peeru\TestCodeBlocks\main.cpp: In static member function 'static void bar::somebar()':
C:\Users\peeru\TestCodeBlocks\main.cpp:14:21: error: 'someFunction' was not declared in this scope
someFunction();
^
Process terminated with status 1 (0 minute(s), 0 second(s))
1 error(s), 2 warning(s) (0 minute(s), 0 second(s))
Any suggestions ?
someFunctionis not visible in Main.cpp. Addextern int someFunction()at the top of Main.cpp, or add in foo.h and include foo.h in Main.cppfoo::someMethodis notstatic, and thus will require afooobject.