class test
{
    virtual void created(){}; //i must do these.
    //or when i call the function the compiler give me an error

    test()
    {
        void created();
    }
}test;



void test::created()
{
    cout << "hello world";
}

these code have 1 error. but how can overrride the created function?

Recommended Answers

All 6 Replies

What are the error(s) you got? The code you posted is not overloading any functions.

line 3: remove the {} characters because it makes the function an inline function. It should look like this:

virtual void created();

line 10 is wrong. Remove the word "test"

in these case case i need the 'test'(line 10). my problem is:
if i don't do 'virtual void created(){};', the line 8 will give me an error;
my obejctive is do a padron function and ,if i need, is overrride ouside of class. is what i need.

error messages:

"C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|17|error: redefinition of 'void test::created()'|
C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|9|error: 'virtual void test::created()' previously defined here|
||=== Build finished: 2 errors, 0 warnings (0 minutes, 2 seconds) ===|"

in these case case i need the 'test'(line 10).

That is not the place to declare an instance of the class. Although it compiles, it's just not normal prace to declare instances like that. And you don't normally want to declare global objects either. See main() for an example of the correct way to declare instance of a class.

Please post the entire program. I think you are missing something. This compiles without warning or errors.

#include <iostream>
class test
{
public:
    virtual void created();

    test()
    {
        void created();
    }
};


void test::created()
{
    std::cout << "hello world";
}

int main()
{
    test t;
}

imagine the class have 4 functions and both are called inside of class. imagine that you only need call 2 functions. that's why i use empty functions inside of class. and ouside i must override what i need to use

why would you write a class eith unused, empty functions??? Just delete the functions.

You can't override a class method from outside a class. You have to do it inside another class that is a child of the base class

class base
{
public:
   virtual void create()
   {
      cout << "Hello from base\n";
    }
 };

 class child : public base
 {
 public:
   virtual void create()
   {
      cout << "Hello from child\n";
    }
 };

Now then, if you don't want base to implement the function then make it a pure virtual function, like this:

    class base
    {
    public:
       virtual void create() = 0; // pure virtual function
     };

In this case, you can not instantiate an instance of base. class base MUST be overriden with a child class.

thanks for all

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.