Hello everyone,
i'm having some problem with the following code:

CODE

inputSouce is an abstract class and the rest are derived from it. The definitions for these are elsewhere.
Now,

inputSource *irSources[4];
irSources[0] = new irSource();

This is giving me errors. i'm using VS2008.

error C2466: cannot allocate an array of constant size 0
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2040: 'irSources' : 'int []' differs in levels of indirection from 'inputSource *[4]'
error C2440: 'initializing' : cannot convert from 'irSource *' to 'int []'

Those are the errors. Could anyone please provide some help as to where i'm going wrong?

PS:

inputSource *irOne = new irSource;

This works fine. i'm getting the problem only when i declare an array of pointers and try to allocate memory to them.

Thanks.

Recommended Answers

All 4 Replies

You can't create abstract objects, hence the term abstract. You can create a pointer to an abstract type but its must point to a derived object.

Here's a simple coded example

#include <iostream>

class expr_node//abstract
{
public:

    virtual ~expr_node() {}

    virtual std::ostream& print(std::ostream & out) const = 0;

};

std::ostream& operator << (std::ostream & out, const expr_node & e)
{
    return e.print(out);
}

class int_node: public expr_node//derived
{
public:

    virtual ~int_node() {}

    std::ostream& print(std::ostream &out) const { out << "derived"; }
};

int main(int argc, char**argv)
{
    expr_node *me[4];

    for (int i = 0; i < 4; ++i)
        me[i] = new int_node;//creating pointer to derived type
    return 0;
}

Thanks for the reply gerard4143.

>>You can't create abstract objects, hence the term abstract. You can create a pointer to an abstract type but its must point to a derived object.

Well,

inputSource *irSources[4];
irSources[0] = new irSource();

Here, irSources is just an array of pointers, similar to me[4] in your example.
It is made to point to a derived object(irSource object). So, that should work right?

Please correct me if im wrong.

It should work if irSource is derived from inputSource and if irSource is not abstract.

class inputSource {
	protected:
			double xPos;
			double yPos;
			double zPos;

			double rawX; 
			double rawY;

	public:
			virtual double getIrX() = 0;	// Pure virtual functions
			virtual double getIrY() = 0;
			virtual double getIrZ() = 0;

			virtual double getRawX() = 0;
			virtual double getRawY() = 0;

			void setIrX(double);
			void setIrY(double);
			void setIrZ(double);

			virtual void calcXYZ() = 0;
};

class irSource : public inputSource {
	public:
			double getIrX();
			double getIrY();
			double getIrZ();

			double getRawX(); 
			double getRawY(); 
			
			void setRawX(double rawX);
			void setRawY(double rawY);
	
			void calcXYZ();
};

class keyboardSource : public inputSource {
	public:
			double getIrX();
			double getIrY();
			double getIrZ();
	
			void calcXYZ();
};

class cameraSource : public inputSource {
	public:
			cameraSource(); 
			~cameraSource();
			
			double getRawX(); 
			double getRawY(); 

			void setRawX(double rawX);
			void setRawY(double rawY);

			double getIrX();
			double getIrY();
			double getIrZ();
	
			void calcXYZ();
};

here are the classes. irSource is derived from inputSource, and is not abstract. So, this is fine right?

Thanks.

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.