What is wrong with this and how do I fix it?
Thanks.

template<typename T>
class Array
{
    public:
	Array(unsigned arraySize):
		data(0), size(arraySize)
	{
		if (size > 0)
			data = new T[size);
	}
	~Array()
	{
		if (data) delete[] data;
	}

	void setValue(unsigned index, const T& value)
	{
		if (index < size)
			data[index] = value;
	}

	T getValue(unsigned index) const
	{
		if (index < size)
			return data[index];
		else
			return T();
	}

    private: T* data;
             unsigned size;
};

Recommended Answers

All 10 Replies

If you don't even know what's wrong with the code, why are you posting it here? Compile it and find out.

If you don't even know what's wrong with the code, why are you posting it here? Compile it and find out.

I don't have a compiler. There's a lot riding on this.

Without compiling I see on line # 9 you have used T[size) instead of T.

>> I don't have a compiler.

Get a compiler. They're free. How are you going to test whatever answers you get from here without a compiler?

Without compiling I see on line # 9 you have used T[size) instead of T.

You're right, that's a typo (on my part) but that's not it. The person who gave me
this on paper claims there's a MAJOR error in it that can be fixed one of 4 ways.

I'm not too sure how much I should say. This post reeks of a homework assignment to me.

I have a feeling that this would be difficult to compile because the declaration is upside-down. Your function implementations are attempting to use member variables before they are declared, which could lead to "undeclared identifier" errors.

>> I have a feeling that this would be difficult to compile because the declaration is upside-down.

It's also difficult to compile without a compiler!

I'm not too sure how much I should say. This post reeks of a homework assignment to me.

I have a feeling that this would be difficult to compile because the declaration is upside-down. Your function implementations are attempting to use member variables before they are declared, which could lead to "undeclared identifier" errors.

Not a homework assignment.

>> I have a feeling that this would be difficult to compile because the declaration is upside-down.

It's also difficult to compile without a compiler!

I'm not saying it's not.

But the upside-down nature could be part of the problem for "the person who gave" the OP this code.

>> I'm not saying it's not.

My comment was aimed at the OP, in a "teach someone to fish" general comment rather than a comment on this particular problem. Without a compiler, it's pointless to to try to figure out what's wrong since the entire process requires changing things, recompiling, etc.

Since I'd already made that point earlier, though, I probably didn't need to post it as a response to your comment.

commented: no worries :) +5
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.