Problem with constructor re-definition

This is a discussion on Problem with constructor re-definition within the C++ Programming forums, part of the General Programming Boards category; Basicly I'm trying to translate Java's collections API ArrayList to a raw version in c++. Code: //ArrayList.h #ifndef _ARRAYLIST_H_ #define ...

  1. #1
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722

    Problem with constructor re-definition

    Basicly I'm trying to translate Java's collections API ArrayList to a raw version in c++.
    Code:
    //ArrayList.h
    #ifndef _ARRAYLIST_H_
    #define _ARRAYLIST_H_
    
    #include /*bla bla bla*/
    
    #define ARRAYLIST_INITIALCAPACITY 128
    
    class ArrayList: public Collection{
    private:
    	void *elements;
    	unsigned int size;
    
    public:
    	ArrayList(int initialCapacity = ARRAYLIST_INITIALCAPACITY);
    	ArrayList(Collection other);
    	~ArrayList();
    
    	ArrayList& operator=(const ArrayList& arr);
    	//methods here...
    };
    #endif //_ARRAYLIST_H_
    
    //ArrayList.cpp
    #include "ArrayList.h"
    
    ArrayList::ArrayList(int initialCapacity){
    	elements = new void[initialCapacity];
    }
    ArrayList::ArrayList(Collection other){}
    ArrayList::~ArrayList(){
    	//delete[] elements;
    	//size=0;
    }
    ArrayList& ArrayList::operator=(const ArrayList& arr){}
    
    //more stuff here
    I'm getting these error with MSVC++ 6... I'm sick of trying to understand what's going on... Bah!!Please help
    Code:
    ArrayList.cpp(3) : error C2535: '__thiscall ArrayList::ArrayList(int)' : member function already defined or declared
            arraylist.h(19) : see declaration of 'ArrayList::ArrayList'
    ArrayList.cpp(6) : error C2535: '__thiscall ArrayList::ArrayList(class Collection)' : member function already defined or declared
            arraylist.h(20) : see declaration of 'ArrayList::ArrayList'
    ArrayList.cpp(7) : error C2535: '__thiscall ArrayList::
    //same errors for operator= and ~ArrayList()
    ArrayList.cpp(13) : fatal error C1004: unexpected end of file found
    teste.cpp
    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Unexepected end of file and re-definition usually means an unmatched { somewhere. I beleve you also need to include an initalizer (int initialCapacity = ARRAYLIST_INITIALCAPACITY) in the defintion(the cpp file). Small constructors are frequenly just written inline, this will bloat compile time and sometimes exe size, but is usually a lot simpler.

    You also cannot use new void[], there is no such thing as a void, you can have a pointer to void, "return" a void or "pass" a void, but these are all ways of saying either do nothing or, in the case of a pointer, have a raw machine address.

    You also may be having a problem in that Container may not have a member named ArrayList.

    Before you tackle pointers to void or arrays of pointers to void, I recomend you build your container out of the existing stl vector The most Javaish way is probably a vector of smart pointers to a common base type. You can also use a vector of pointers to objects decended from a type with a virtual clone() method, that will allow deep copying.

  3. #3
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Thanks for the help, but your mixing on stuff I still haven't considered... the problem is that the compiler is, stupidly, assuming that I defined the same constructors and operator twice. Plus I have another class, in the same workspace that's written exactly the same way, and compiles well.
    And the "int initialCapacity = ARRAYLIST_INITIALCAPACITY" isn't necessary in the cpp file.

    This compiles well. The above class, that is wirtten the same way, doesn't!!
    Code:
    //GlutWindow.cpp
    #include "GlutWindow.h"
    
    GlutWindow::GlutWindow(char *_title) : title(_title){
    	initState();
    }
    //GlutWindow.h
    class GlutWindow{
    private:
    //stuff here
    public:
    	//contructores
    	GlutWindow(char *_title = "");
    //more stuff here
    };
    And I still don't know to use vectors...
    //EDIT
    I meant void*[]... that's a detail for later.
    Last edited by xErath; 07-06-2004 at 11:14 PM.

  4. #4
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    I solved it... incredible.. A single comma made all that noise!!! bah! thankx

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Quote Originally Posted by xErath
    I solved it... incredible.. A single comma made all that noise!!! bah! thankx
    c/c++ compilers generally continue to try to compile, even though its obvious that the wheels have come off, it's kind of cute. Although, if the compiler could produce an "you forgot a comma here" error, you wouldn't need to include any commas .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. HELP!! Function Definition problem? Syntax Error I think..
    By felixgun in forum C++ Programming
    Replies: 12
    Last Post: 11-04-2006, 03:49 AM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. Replies: 5
    Last Post: 11-07-2005, 10:34 PM

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