Hey guys, just wonderin why I am getting a compiler error on this code... I've had this error before and spent ages tryin to fix it.... But i can tfor the life of me remember... Thanks to all who reply...:)

class Date
{
    public:
    Date();
    private:
};
class Time
{
    public:
    Time();
    private:
};
class Product
{
    public:
    Product();
    private:
};
int main()
{
    Collectable* collected[4];
    collected[0] = new Date();
    collected[1] = new Time();
    collected[2] = new Product();
    collected[3] = new Date();
    return 1;
}

[TEX]
Error:
error: `collected' was not declared in this scope
error: `Collectable' was not declared in this scope
[/TEX]

Dani AI

Generated

Short diagnosis and fix: the compiler error happens because the name Collectable does not exist when you try to declare the array of pointers. That first error prevents the collected array from being declared, so later uses look like "not declared" — a cascading error. was correct: the usual solution is to provide a Collectable base class and make Date, Time and Product inherit from it. 's pointer comment is also relevant: pointers must refer to valid objects (created with new or, better, smart pointers).

Minimal example of a safe polymorphic base (add whatever virtual interface you need):

class Collectable {
public:
    virtual ~Collectable() {}
    virtual void display() const = 0; // optional interface
};

class Date : public Collectable {
public:
    Date() {}
    void display() const override { /* show date */ }
};

Modern, safer usage (no raw new, automatic lifetime management):

#include <vector>
#include <memory>

std::vector<std::unique_ptr<Collectable>> collected;
collected.emplace_back(std::make_unique<Date>());
collected.emplace_back(std::make_unique<Time>());

Quick troubleshooting checklist:

  • Ensure Collectable is declared before you use Collectable* collected[...]. A forward declaration (class Collectable;) lets you declare pointers, but you need the full definition to construct or call methods.
  • Give the base class a virtual destructor if you delete derived objects through base pointers.
  • If you declare constructors (e.g., Date();) make sure you also define them somewhere or you will get linker errors.
  • Compile with warnings on (for example -std=c++11 -Wall -Wextra) and read the first error — it almost always points to the real problem.
  • Convention: return 0 from main() for success.

Recommended Answers

All 5 Replies

There is no mention of what a Collectable represents.
Then it looks like your classes need to be inheriting from Collectable in order for the allocations/assignments to work.

Would it help if i made a collectable class?

Would it help if i made a collectable class?

Errm, I think you need to figure out what you want to achieve - from the ground up.
If you are practising inheritance/pointers - perhaps find some basic tutorials.

OK thanks for your help mitrmkar..

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.