vividexstance's Profile User Rating: *****

Reputation: 783 Master
Active Posts:
2,771 (1.42 per day)
31-December 10
Profile Views:
Last Active:
User is offline Apr 26 2016 06:17 AM

Previous Fields

OS Preference:
Favorite Browser:
Favorite Processor:
Favorite Gaming Platform:
Your Car:
Who Cares
Dream Kudos:
Expert In:

Latest Visitors

Icon   vividexstance has not set their status

Posts I've Made

  1. In Topic: template class error

    Posted 24 Apr 2016

    That won't fix all the errors however. Since the stream operators need to be friends, then you need to forward declare the Array class and those functions before you declare them as friends of the Array class. Just put this before the Array class:
    template <typename T, int size>
    class Array;
    template <typename T, int size>
    ostream &operator<<(ostream &, const Array<T, size> &);
    template <typename T, int size>
    istream &operator>>(istream &, Array<T, size> &);
  2. In Topic: template class error

    Posted 24 Apr 2016

    The problem with your stream operators is that you don't need the template line, this:
    template <typename T, int size>
    friend ostream &operator<<(ostream &, const Array<T, size> &);
    template <typename T, int size>
    friend istream &operator >> (istream &, Array<T, size> &);

    The compiler is saying that the template parameters are "shadowing" the class' template parameters. SO you get rid of the template line, but you need to add empty <> to the function's signature:
    friend ostream &operator<< <>(ostream &, const Array<T, size> &);
    friend istream &operator>> <>(istream &, Array<T, size> &);
  3. In Topic: template class error

    Posted 24 Apr 2016

    Well the last function has a semicolon after the brace, that's the first error.

    Some of the errors you're getting are from the original class not the float version, so maybe fix that before you move on to the specialization.
  4. In Topic: Print an array of strings and ignore nulls

    Posted 24 Apr 2016

    Jim is talking about the C11 standard, which is 5-6 years newer than the ones you're posting.
  5. In Topic: template class error

    Posted 24 Apr 2016

    It's not enough to just define the member functions with float instead of the template parameter. You have to define the whole class as well.

    template <int size>
    class Array {
    	/* ATTENTION! friend operator templates must be specialized */
    	template <int size>
    	friend ostream &operator<<(ostream &, const Array<float, size> &);
    	template <int size>
    	friend istream &operator>>(istream &, Array<float, size> &);
    	Array(const Array<float, size> &);
    	int getSize() const;
    	const Array<float, size> &operator=(const Array<float, size> &); // assignment operator
    	bool operator==(const Array<float, size> &) const; // equality operator
    	// inequality operator; returns opposite of == operator
    	bool operator!=(const Array<float, size> &right) {
    		return !(*this == right); // invokes Array::operator==
    	// subscript operator for non-const objects returns modifiable lvalue
    	float &operator[](int);
    	// subscript operator for const objects returns rvalue
    	float operator[](int) const;
    	float ptr[size]; // pointer to first element of pointer-based array

    And then the member function definitions of course.

    View Posthorace, on 24 April 2016 - 08:31 AM, said:

    post #7 stated the exercise required to write a template for Array class, then override the template with a specific definition for float type

    And that's exactly what a template specialization does.


Page 1 of 1
  1. Photo

    orihotomihsah Icon

    09 Sep 2013 - 09:45
Page 1 of 1