error: 'myfunction' not declared in this scope

Hello, wondering if anyone can help me. Using CodeBlocks 12.11. I've been trying to write a program using wxWidgets and finally gave up because... well, it was much less fun than I imagined. (Never "GUIed" before - yep, just coined that) I gave up and tried to rewrite it the old fashioned way. I started with a basic skeleton and cannot get it to compile which is making me wonder if I've screwed up my configurations someplace. (I hate to muck with the configurations too much because my wxWidgets code is finally compiling after many agonizing attempts.) Do I have to have completely different configurations if I want to leave out the interface? The other possibility is that I've completely lost it, and hours of tyring to "GUI" has made my brain gooey... ;) that was so cheezy it made me blush... Any comments will be greatly appreciated, at my expense or otherwise. :) Okay to the point...

Can somebody tell me if there is a major error in my code or suggest a solution? Here we go...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
///////////Main.cpp/////////////////////////
#include <cstdlib>
#include <iostream>
#include "FnLib.h"

using namespace std;


int main()
{

   Hello();
    return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
///////////FnLib.h/////////////////////////////
#ifndef FNLIB_H
#define FNLIB_H


class FnLib
{
    public:
        FnLib();
        virtual ~FnLib();
        void Hello();
    protected:
    private:
};

#endif // FNLIB_H 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
///////////FnLib.cpp///////////////////////
#include "FnLib.h"
#include <iostream>

using namespace std;

FnLib::FnLib()
{
    //ctor
}

FnLib::~FnLib()
{
    //dtor
}
void FnLib::Hello()
{
    cout<<"hello";
}
/////////////////////////////////////// 

Here's the error---> error: 'Hello()' not declared in this scope

I'm pretty novice, so a descriptive response would be greatly appreciated. I'm not yet fluent in programese.

Thank you, thank you!!
You havent declared any objects of "FnLib" in your main() function. For you to use "Hello" you should probably do something like this in your main() body:
1
2
3
4
5
6
7
8
9
10
#include <cstdlib>
#include <iostream>
#include "FnLib.h"
using namespace std;

int main() {
    FnLib object;       // Or whatever you want to call it
    object.Hello();
    return 0;
} 


The reason is that it doesn't know where to look to find "Hello()", so you need to give it an object of FnLib and tell it to use that objects function "Hello()".
Last edited on
Thank you so so much!

How then can I create a library of functions where I don't have to declare an object first? It seems I've done that in other programs and I'm wondering what the difference is. My intention was to create a library and not really an object and I seem to have really confused myself. Can I nix the creator and destructor? I used the class wizard to create the class.

I'm really appreciate your help!
Last edited on
You don't need a class at all, so eliminate the class FnLib. Instead, place a forward declaration for the function Hello() in the header file and define it in the code file.

1
2
3
4
5
6
7
8
9
///////////FnLib.h/////////////////////////////
#ifndef FNLIB_H
#define FNLIB_H

#include <iostream>

void Hello();

#endif // FNLIB_H  


1
2
3
4
5
6
7
8
9
10
///////////FnLib.cpp///////////////////////
#include "FnLib.h"

using namespace std;

void Hello()
{
    cout<<"hello" << endl;
}
///////////////////////////////////////  


Note that I moved the line #include <iostream> from the .cpp file to the .h file which is a better place for it. In addition I added endl to the cout statement to flush the buffer and move the cursor down to the next line. You can add additional functions to FnLib using the same technique, a forward declaration in the header file and the implementation of the function in the code file.

You can also eliminate the line #include <iostream> in Main.cpp since you have already included it in FnLib.h, though it doesn't hurt anything to have it there since there will be header guards in <iostream> to prevent double inclusion.


That's it! Thank you, thank you!! (I blame the wizard ;) )
<thread drift>
@Alrededor said

Note that I moved the line #include <iostream> from the .cpp file to the .h file which is a better place for it.


I completely disagree with the end of this statement. There is nothing in the FnLib.h header that requires anything declared in iostream. By including iostream in this header file, every file that includes FnLib.h will include iostream also. In more sophisticated programs, this can lead to unnecessary dependencies and unnecessarily long rebuild times. While the effect is trivial in this toy program, this strategy would not be considered good programming practice.

A header file should include only headers that it needs to define its classes and declare its functions. A header file should never include extraneous header files -- the fewer included files the better. In this case, the iostream header file should be included in FnLib.cpp as originally proposed, because that's the file that uses items defined in iostream.

You can also eliminate the line #include <iostream> in Main.cpp since you have already included it in FnLib.h


I agree that iostream should not be included in Main.cpp, but not for the reason stated. The iostream header should be removed from Main.cpp because nothing in Main.cpp requires anything from iostream.

</thread drift>
Last edited on
doug4-
I never quite thought of those details. Thanks for the correction.
Topic archived. No new replies allowed.