how can I use a function inside an object?
(the code is too long to put here but here's a replica)

class example {
    public:
    example();
    ~example();
    
    void doSomthing() {
        afunction();
    }

}

void afunction() {
    //do more things
}

in this example i wouldnt NEED to use a function but I do need to in my code.
when I try to compile the program I get errors like:
error: 'functionName' was not declared in this scope. and
error: unused variable 'functionName'

Edited 7 Years Ago by MattyRobot: n/a

You would use the function just like you would inside another function. With the example code afunction isn't defined (there is no prototype.) You could do something like this

void afunction(); // function prototype

class example {
    public:
    example();
    ~example();
    
    void doSomthing() {
        afunction();
    }

};

void afunction() {
    //do more things
}

Edited 7 Years Ago by ShawnCplus: n/a

You can call a function provided it has already been declared:

void afunction();

class example {
    public:
    example();
    ~example();
    
    void doSomthing() {
        afunction();
    }
};

void afunction() {
    //do more things
}

A definition is also a declaration, so you can move the definition above your class definition for the same effect:

void afunction() {
    //do more things
}

class example {
    public:
    example();
    ~example();
    
    void doSomthing() {
        afunction();
    }
};

Note the semicolon at the end of the class definition that I added. That is important, and forgetting it usually causes incomprehensible errors. ;)

Edited 7 Years Ago by Tom Gunn: n/a

or you can use pointer member function, and pass the class a function
to call.

#include <iostream>

using namespace std;
 

void callThisFunc() { cout<<"This function was called"; }

class CallFunc
{
	
	typedef void(*pF)();

private :
	int x;
public:
	CallFunc() : x(100){ }
	CallFunc(int xo) : x(xo) { }

	void callSomeFunc(pF yourFunc) { yourFunc();cout<<" and x is : "<<x<<endl; }

};

int main()
{
	CallFunc cF(123);

	cF.callSomeFunc(callThisFunc);
 
    return 0;

}

Edited 7 Years Ago by firstPerson: n/a

Yes sorry, I wrote that in a hurry, but that isn’t the problem with the other code. I know that sort of defeats the object of the example but with the code I wrote I had included all the things you mentioned and still didn’t work. the function had a prototype and the class had a semicolon. but the compiler said that the function was not declared in the scope. however i will look through the code again and see if it was somthing else

the code I wrote I had included all the things you mentioned and still didn’t work. the function had a prototype and the class had a semicolon. but the compiler said that the function was not declared in the scope.

You could also post the actual code here, so we can help you instead of continue guessing. :icon_wink:

Edited 7 Years Ago by Nick Evan: n/a

There is a possibility that a function you're using is inside some
namespace, like std::cout.

OK, ill give you the whole poject, this is my first big project in c++ (I know I'm stupid and shouldn't start big, but it worked until I added classes)
be warned its over 1600 lines of code (and I don't want to waste too much of your time)

Attachments
/* **********************************
*      my first ever c++ game       *

title: Pet Keeper
description:manage a group of pets
date start: 16/08/2009
date finish:
copywrite: none
company: Gazelle gaming
programmers: MatthewB;
confidential: 0/10
average rating: 0% (0 reviews)
type: open source console application
genre:
content rating: 3+
difficulty rating:


***********************************/
#include <iostream> //to interact with the console
#include <string> //to use the string variable
#include <fstream> //to work with files
#include <vector> //to use the vector dynamic array
#include <cstdlib> //to genarate random numbers
#include <time.h> //to use the time as the random number seed

class Pet {
public:
    //constructor
    Pet(std::string Name, unsigned long Age, char Gender, std::string Species) {
        Name = this->Name;
        Age = this->Age;
        Gender = this->Gender;
        Species = this->Species;
    }
    //desructor
    ~Pet() {}

    //attributes
    std::string Name;
    unsigned long Age;
    char Gender;
    std::string Species;

    void Edit(unsigned short x) {
        cls();
        std::cout<< "\n"
        << "press 1: to edit the Name: " << Pets[x].Name << "\n"
        << "press 2: to edit the Age: " << Pets[x].Age << "\n"
        << "press 3: to edit the Gender: " << Pets[x].Gender << "\n"
        << "press 4: to edit the species: " << Pets[x].Species << "\n\n"
        << "or press 5: to edit everythig\n";

        char EditSelection;
        std::cin>> EditSelection;
        if(!std::cin.good()) {
            std::cin.clear();
            delete &EditSelection;
            cls();

            std::cout<< "the input is invalid\n"
            << "press enter to start again";
            EmptyBuffer();
            std::cin.get();
            EditPet();
        }

        switch(EditSelection) {

            case '1':
                cls();
                std::cout<< "\n" << "Enter the pets name:";
                std::cin >> Pets[x].Name;
                if(!std::cin.good()) {
                    std::cin.clear();
                    cls();

                    std::cout<< "the input is invalid\n"
                    << "press enter to start again";
                    EmptyBuffer();
                    std::cin.get();
                    cls();
                    EditPet();
                }
            break;

            case '2':
                cls();
                std::cout<< "Enter the pets age:";
                std::cin >> Pets[x].Age;
                if(!std::cin.good()) {
                    std::cin.clear();
                    cls();

                    std::cout<< "the input is invalid\n"
                    << "press enter to start again";
                    EmptyBuffer();
                    std::cin.get();
                    cls();
                    EditPet();
                }
            break;

            case '3':
                cls();
                std::cout<< "Enter the pets gender (M/F):";
                std::cin >> Pets[x].Gender;
                if(!std::cin.good()) {
                    std::cin.clear();
                    cls();

                    std::cout<< "the input is invalid\n"
                    << "press enter to start again";
                    EmptyBuffer();
                    std::cin.get();
                    cls();
                    EditPet();
                }

                switch(Pets[x].Gender) {

                    case 'm':
                        Pets[x].Gender = 'M';
                    break;

                    case 'f':
                        Pets[x].Gender = 'F';
                    break;

                    case 'M':
                    case 'F':
                    break;

                    default:
                        cls();

                        std::cout<< "the input is invalid\n"
                        << "press enter to start again";
                        EmptyBuffer();
                        std::cin.get();
                        cls();
                        EditPet();
                    break;
                }
            break;

            case '4':
                cls();
                std::cout<< "Enter the pets species:";
                std::cin >> Pets[x].Species;
                if(!std::cin.good()) {
                    std::cin.clear();
                    cls();

                    std::cout<< "the input is invalid\n"
                    << "press enter to start again";
                    EmptyBuffer();
                    std::cin.get();
                    cls();
                    EditPet();
                }
            break;

            case '5':
                cls();
                std::cout<< "\n" << "Enter the pets name:";
                std::cin >> Pets[x].Name;
                if(!std::cin.good()) {
                    std::cin.clear();
                    cls();

                    std::cout<< "the input is invalid\n"
                    << "press enter to start again";
                    EmptyBuffer();
                    std::cin.get();
                    cls();
                    EditPet();
                }

                std::cout<< "Enter the pets age:";
                std::cin >> Pets[x].Age;
                if(!std::cin.good()) {
                    std::cin.clear();
                    cls();

                    std::cout<< "the input is invalid\n"
                    << "press enter to start again";
                    EmptyBuffer();
                    std::cin.get();
                    cls();
                    EditPet();
                }

                std::cout<< "Enter the pets gender (M/F):";
                std::cin >> Pets[x].Gender;
                if(!std::cin.good()) {
                    std::cin.clear();
                    cls();

                    std::cout<< "the input is invalid\n"
                    << "press enter to start again";
                    EmptyBuffer();
                    std::cin.get();
                    cls();
                    EditPet();
                }

                switch(Pets[x].Gender) {

                    case 'm':
                        Pets[x].Gender = 'M';
                    break;

                    case 'f':
                        Pets[x].Gender = 'F';
                    break;

                    case 'M':
                    case 'F':
                    break;

                    default:
                        cls();

                        std::cout<< "the input is invalid\n"
                        << "press enter to start again";
                        EmptyBuffer();
                        std::cin.get();
                        cls();
                        EditPet();
                    break;
                }

                std::cout<< "Enter the pets species:";
                std::cin >> Pets[x].Species;
                if(!std::cin.good()) {
                    std::cin.clear();
                    cls();

                    std::cout<< "the input is invalid\n"
                    << "press enter to start again";
                    EmptyBuffer();
                    std::cin.get();
                    cls();
                    EditPet();
                }

            break;

            default:
                cls();
                std::cout<< "the input is invalid \n"
                << "press enter to return to the main menu";
                EmptyBuffer();
                std::cin.get();
                DisplayMenu();
            break;


                    }

        cls();
        std::cout<< "Pet sucsessfully Edited.\n"
        << "the new data is:\n\n"

        << "Name: " << Pets[x].Name << "\n"
        << "Age: " << Pets[x].Age << "\n"
        << "Gender: " << Pets[x].Gender << "\n"
        << "Species: " << Pets[x].Species << "\n\n"

        << "press enter to return to the main menu";
        EmptyBuffer();
        std::cin.get();
        DisplayMenu();
    }

};

class Activity {
    public:

    //constructor
    Activity(std::string Text, std::string Species, bool ForAll) {
        Text = this->Text;
        Species = this->Species;
        ForAll = this->ForAll
    }
    //destructor
    ~Actvity() {}

    //attributes
    std::string Text;
    std::string Species;
    bool ForAll;


};

/*
struct pet{
    std::string Name;
    unsigned long Age;
    char Gender;
    std::string Species;
};
*/
struct Activity{
    std::string Text;
    std::string Species;
    bool ForAll;
};

//declare global variables
std::vector<pet> Pets;
std::vector<Activity> Activities;
unsigned long GameTime;

//declare function prototypes
void Save(void);
void Load(void);

void AddPet(void);

void AddActivity(void);
void EditActivity(void);

void ListPets(void);
void ListActivities(void);


void DisplayMenu(void);
void DisplayHelp(void);
void DeleteMenu(void);

void cls(void);
void EmptyBuffer(void);
void Sleep(unsigned int mseconds);

void RunSimulation(void);



int main(void) {

    DisplayMenu();

return 0;
}




//functions

//pet functions
void AddPet(void) {
    cls();
    pet TmpPet; //load all the data into a temporary variable

    std::cout << "you chose to add a new pet!" << "\n";

    EmptyBuffer();
    std::cout<< "\n" << "Enter the pets name:";
    std::getline(std::cin, TmpPet.Name);
    if(!std::cin.good()) {
        std::cin.clear();
        delete &TmpPet;
        cls();

        std::cout<< "the input is invalid\n"
        << "press enter to start again";
        EmptyBuffer();
        std::cin.get();
        AddPet();
    }

    std::cout<< "Enter the pets age:";
    std::cin >> TmpPet.Age;
    if(!std::cin.good()) {
        std::cin.clear();
        delete &TmpPet;
        cls();

        std::cout<< "the input is invalid\n"
        << "press enter to start again";

be warned its over 1600 lines of code

The problem is as we said at first. You are using things before they are declared. The cls() function is one example. You use it in the definition of the Pet class, but it is declared after the Pet class definition. Think of the declare before use rule as being top-down. If you flatten out your code into one file, the declaration has to be above the first use.

you know i just came to that conclusion when I reset the code to an earlier date when it worked, and remade the class, it didnt work so i put it where i put the pet struct! and it worked.

(I'm learning)
and the reason I didn't figure out the bug in the fist place is because I didn't know whether I could use functions inside a class

and thanks for everyones help ;)

Edited 7 Years Ago by MattyRobot: i wanted to add a bit

Here is start of that main.cpp, which resolves some serious
problems you had with your code. First of all, you have to read
C++ tutorial once more, and try to grasp the basic idea of the
language. Study forward declarations, when to use new/delete,
how should design classes, etc. Believe me, more you study now,
more easier it will be in the long run!

#include <iostream> //to interact with the console
#include <string> //to use the string variable
#include <fstream> //to work with files
#include <vector> //to use the vector dynamic array
#include <cstdlib> //to genarate random numbers
#include <time.h> //to use the time as the random number seed

//forward declarations
class Pet;
class Activity;

//declare global variables
std::vector<Pet> Pets;
std::vector<Activity> Activities;
unsigned long GameTime;

//declare function prototypes
void Save(void);
void Load(void);

void AddPet(void);

void AddActivity(void);
void EditActivity(void);

void ListPets(void);
void ListActivities(void);


void DisplayMenu(void);
void DisplayHelp(void);
void DeleteMenu(void);

void cls(void);
void EmptyBuffer(void);
void Sleep(unsigned int mseconds);

void RunSimulation(void);

class Pet {
public:
	Pet() {}
    //constructor
    Pet(std::string Name, unsigned long Age, char Gender, std::string Species) {
        this->Name = Name;
        this->Age = Age;
        this->Gender = Gender;
        this->Species = Species;
    }
    //desructor
    ~Pet() {}

    //attributes
    std::string Name;
    unsigned long Age;
    char Gender;
    std::string Species;

	void EditPet() {}

    void Edit(unsigned short x) {
        cls();
        std::cout<< "\n"
        << "press 1: to edit the Name: " << Pets[x].Name << "\n"
        << "press 2: to edit the Age: " << Pets[x].Age << "\n"
        << "press 3: to edit the Gender: " << Pets[x].Gender << "\n"
        << "press 4: to edit the species: " << Pets[x].Species << "\n\n"
        << "or press 5: to edit everythig\n";

        char EditSelection;
        std::cin>> EditSelection;
        if(!std::cin.good()) {
            std::cin.clear();
            //delete &EditSelection;
            cls();

. . .

Edited 7 Years Ago by rughead: n/a

as I said i am new to classes, with a few minutes of tinkering I moved the implamentation of pet.edit() to after the function declarations and the class to above the functions but below the global variables. that is a better way though, I will change it. thanks for teaching me something
:)

Comments
Thanks for using code tags, and for being nice :)
This question has already been answered. Start a new discussion instead.