templates problems give compile error

Discussion in 'C++' started by Tony Johansson, Aug 22, 2005.

  1. Hello Experts!

    Why do I get compile error here in this small class template. The compiler
    error give this error
    c:\Documents and Settings\Tony\kau\cplusplus\test12\start.cpp(9): error
    C2440: '=' : cannot convert from 'int *' to 'int'

    It's something that I have forgot in the class template..

    #include <iostream>
    template<typename T, int size>
    class Array
    {
    public:
    const int& operator[](int pos) const { return value[pos]; }
    int& operator[](int pos) { return value[pos]; }
    private:
    T value[size];
    };

    #include <iostream>
    #include "array.h"
    using namespace std;
    int main()
    {
    Array<int*,1> array1, array2;
    array1[0] = new int(3);
    return 0;
    }

    Many thanks

    //Tony
     
    Tony Johansson, Aug 22, 2005
    #1
    1. Advertising

  2. Tony Johansson

    Guest

    I'm not sure if this is a homework problem or not, so my answer will be
    deliberately vague hint--but quite useful, for all that.

    What is operator[] returning?

    Once you figure it out (and remove the unused array2 which you're not
    using), it compiles and runs cleanly under g++ using -Wall -W -ansi
    -pedantic.

    You should also make it a point to delete your pointer when you're done
    using it. If you're going to manually allocate memory, you need to get
    in the habit of taking responsibility for deallocating it when you're
    done with it.
     
    , Aug 22, 2005
    #2
    1. Advertising

  3. Tony Johansson

    Ian Guest

    Tony Johansson wrote:
    > Hello Experts!
    >
    > Why do I get compile error here in this small class template. The compiler
    > error give this error
    > c:\Documents and Settings\Tony\kau\cplusplus\test12\start.cpp(9): error
    > C2440: '=' : cannot convert from 'int *' to 'int'
    >
    > It's something that I have forgot in the class template..
    >
    > #include <iostream>
    > template<typename T, int size>
    > class Array
    > {
    > public:
    > const int& operator[](int pos) const { return value[pos]; }

    So if T is int*, you are attempting to return an int* form a function
    that returns an int&.

    Should it be T&?

    Ian
     
    Ian, Aug 22, 2005
    #3
  4. Tony Johansson

    Guest

    the problem lies in the statement::

    array1[0] = new int(3);

    on the data type level this will evalueate to

    int = int*; //since array1[0] returns an int rather than int *

    so this is giving the error message!!!


    thanks
    rt
     
    , Aug 22, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JKop
    Replies:
    3
    Views:
    519
  2. grocery_stocker
    Replies:
    10
    Views:
    658
    Keith Thompson
    May 25, 2005
  3. Replies:
    5
    Views:
    466
    mlimber
    Oct 12, 2005
  4. recover
    Replies:
    2
    Views:
    855
    recover
    Jul 25, 2006
  5. Nagaraj
    Replies:
    1
    Views:
    912
    Lionel B
    Mar 1, 2007
Loading...

Share This Page