0

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;
};
5
Contributors
10
Replies
11
Views
6 Years
Discussion Span
Last Post by VernonDozier
Featured Replies
  • >> 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, … Read More

0

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

0

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.

0

>> 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?

0

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.

0

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.

0

>> 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!

0

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.

0

>> 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.

1

>> 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.

Comments
no worries :)
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.