0

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 ?

2
  • someFunction is not visible in Main.cpp. Add extern int someFunction() at the top of Main.cpp, or add in foo.h and include foo.h in Main.cpp Commented Feb 26, 2015 at 15:17
  • Also of note: foo::someMethod is not static, and thus will require a foo object. Commented Feb 26, 2015 at 15:20

4 Answers 4

1

As the compiler says, someFunction hasn't been declared in main.cpp, only in a separate translation unit, foo.cpp. Functions need to be declared before use.

Add a declaration in either main.cpp, or a header included from both:

int someFunction();

You also need to return something from the functions that claim to return int, as the other warnings say.

Sign up to request clarification or add additional context in comments.

Comments

1

You have to provide a declaration for the function in main.cpp too, modify the foo.h header accordingly:

#ifndef FOO_H_INCLUDED
#define FOO_H_INCLUDED

#include <iostream>

int someFunction();

struct foo
{
    int someMethod();
};


#endif // FOO_H_INCLUDED

and add #include "foo.h" in main.cpp.

I am not sure why MSVC++ compiled without complaining, though.

1 Comment

That was what I was curious about.
0

Method someFunction and all others are declared as returning type int. So, add return 0 at the end of functions or declare them as void. And you cannot call non static function from static function.

4 Comments

That explains the warnings; but the question is about the error.
That has totally nothing to do with the answer
"you cannot call non static function from static function" - nonsense, of course you can. The only problem here is that the function hasn't been declared.
I know it looks simple. Visual Studio has no problem with it
0

You declared someFunction() in foo.cpp, but not in main.cpp. When main.cpp is compiling, it doesn't know the declaration of someFunction(), so it fails.

You need to:

  1. add int someFunction(); to the top of main.cpp

  2. add int someFunction(); to the foo.h file, and then #include "foo.h" in main.cpp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.