assignment to `char *' from `int *'

This is a discussion on assignment to `char *' from `int *' within the C++ Programming forums, part of the General Programming Boards category; Hello, Thanks in advance for anyone's help and/or advice, it is greatly apprecaited on what, i am sure, is a ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    24

    assignment to `char *' from `int *'

    Hello,

    Thanks in advance for anyone's help and/or advice, it is greatly apprecaited on what, i am sure, is a stupid question. Nevertheless, here is my problem:

    I have a template class called "MyArray" and i want it to be able to accept an int or char. The contents of the array and the size of the array is taken as a command line argument.

    Here is the code in the main function:
    Code:
    #include "MyArrayDefs.h"
    
    using namespace std;
    
    void main( int argc, char* argv[] )
    {
            MyArray<char> charArr( strlen( argv[ 1 ] ) );
            MyArray<int> intArr( strlen( argv[ 1 ] ) );
    }
    Here is the header file:
    Code:
    template < class Type > class MyArray
    {
            public:
                    MyArray( const int ); 
                    ~MyArray();
            private:
                    char* base;
                    int size;
    };
    Here is the Template header file:

    Code:
    #include <iostream>
    #include "MyArray.h"
             
    using namespace std;
            
    template < class Type >
    MyArray < Type > :: MyArray( const int n )  
    {
            base = new Type [ n ];     
            size = n;
    }
    When i create an array like this:
    Code:
    MyArray<char> charArr( strlen( argv[ 1 ] ) );
    it works fine. But when i do this:
    Code:
    MyArray<int> intArr( strlen( argv[ 1 ] ) );
    it does not work fine. It comes up with the following error:

    MyArrayDefs.h: In method `MyArray<int>::MyArray(int)':
    main.cc:15: instantiated from here
    MyArrayDefs.h:14: assignment to `char *' from `int *'

    Any help in solving this error would be massively appreciated.

    Best regards, global

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hi,

    Code:
    template < class Type > class MyArray
    {
            public:
                    MyArray( const int ); 
                    ~MyArray();
            private:
                    char* base;
                    int size;
    };
    Code:
    template < class Type >
    MyArray < Type > :: MyArray( const int n )  
    {
            base = new Type [ n ];     
            size = n;
    }
    So, this:

    MyArray<int> intArr( strlen( argv[ 1 ] ) );

    produces:

    base = new int[n];
    Last edited by 7stud; 04-25-2005 at 03:54 AM.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    24
    Thanks very much. I didn't look at my code properly. Thanks for your time and help.


    Best regards, global

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    > void main( int argc, char* argv[] )
    You know this is wrong - use int main
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  4. invalid conversion from `char*' to `char'
    By Tommy7 in forum C++ Programming
    Replies: 3
    Last Post: 06-09-2005, 10:45 PM
  5. parse error before `char'
    By Yourhighness in forum C Programming
    Replies: 6
    Last Post: 06-14-2003, 01:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21