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'

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
}

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. ;)

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;

}

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:

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)

have fun (sarcastic)

Hahahahahhaha!
Very nice Matty!

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 ;)

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();

. . .

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
:)

commented: Thanks for using code tags, and for being nice :) +22

Yr welcome. Keep on programming with C++, it is a wonderful language! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.