There's a header class called example.h

#ifndef __Sample_H__
#define __Sample_H__

namespace example
{
        namespace Samples
        {
                class AlgorithmSample 
                {
                public:

                        virtual void release(void) { delete this; }
                        virtual boolean process(void);

				};
				
				class AlgorithmDesc 
                {
                public:

                        virtual String getName(void) const                { return "some example program"; }


				};
        };
};

#endif

the example.cpp file

#include "Sample.h"
#include <iostream>

boolean Sample::process(void)
{
std::cout<<" printed this !! " <<std::endl;
return true;

}

Then i need to call the above process method from another class, how should i do that

for example say, we have a class called Animal.cpp , and i need to call the process() function of sample.cpp class from the process() function of Animal.cpp class

Hope i didn't confuse you'l, in a nutshell both Animal.cpp and sample.cpp has a Process() function, and i need to call the process() function of sample.cpp from Animal.cpp's process() function.

Please help :(

Hi,

Firstly, the boolean type is just bool in C++. Using boolean will give you a compile error.

virtual bool process(void);

Also, in your cpp you have just put boolean Sample::process(void) instead of

bool AlgorithmSample::process(void)

As for calling it, you must make an instance of the object then call it from there.

AlgorithmSample mySample;
mySample.process();

I hope that answers your query.

Colezy

Hi,

As for calling it, you must make an instance of the object then call it from there.

AlgorithmSample mySample;
mySample.process();

I hope that answers your query.

Colezy

i get this error message

error C2228: left of '.process' must ha
ve class/struct/union
type is ''unknown-type''

when i coded as below;

Example ex;

ex.process();

What should i do ? and what does the error message say? :(

The error is saying that the word to the left of variable 'ex' is undefined. In other words it doesn't know what an 'Example' is.
You class is AlgorithmSample not Example, Example is your namespace.
Why do you have namespaces?
As it is at the minute you either have to use the command

#include "example.h"
using namespace example::Samples

Or declare your object like this:

#include "example.h"

//....
//....

example::Samples::AlgorithmSample ex;
ex.process();

Imagine the namespaces like folders in a file structure, you can't access what is inside until you tell it where to look.

Regards,

Colezy

You will need to include the header file first and then either access the namespaces using the scope resolution operator '::' or add "using namespace /*namespace*/" (which is particularly useful if you will be using the namespace functionality often).

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.