Thread: Compilation error wrt types + constructors

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    8

    Compilation error wrt types + constructors

    Hi Everyone.
    I am new to c++, and trying to complete an assignment. I have to create a class called 'Element', declare it in a header, implement it in a c++ file. When i try and compile it to object code (-c flag on g++), I get the following error:

    g++ -c Element.cpp
    Element.cpp:5: error: new types may not be defined in a return type
    Element.cpp:5: error: return type specification for constructor invalid


    here is the code for the .h:

    Code:
    //Element.h an item implementation to hold any queue/stack data.
    #ifndef ELEMENT_H
    #define ELEMENT_H
    
    class Element{
     public:
      Element(int* newItem);
      int* getItem();
      void setNext(Element*);
      Element * getNext();
    
     private:
      int * item;
      Element* next;
    }
    
    #endif
    and the .cpp:

    /
    Code:
    /Element.cpp implementation to hold data element
    #include <stdio.h>
    #include "Element.h"
    
    Element::Element(int * newItem){
      item = newItem;
    }
    
    int* Element::getItem(){
      return item;
    }
    
    void Element::setNext(Element* newNext){
      next = newNext;
    }
    
    Element* Element::getNext(){
      return next;
    }
    all the research i have done on this compilation error points to the lack of a semi-colon after a struct... so I really dont know what im looking for. Any ideas on how to fix this?

    In addition, I later have to alter this class so it can store any type, not just 'int', using a template. Can anyone tell me where I can start looking to figure out how to do that?

    Thanks
    Last edited by thebudbottle; 03-05-2005 at 07:38 PM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Common mistake, forgot the semicolon:

    Code:
    //Element.h an item implementation to hold any queue/stack data.
    #ifndef ELEMENT_H
    #define ELEMENT_H
    
    class Element{
     public:
      Element(int* newItem);
      int* getItem();
      void setNext(Element*);
      Element * getNext();
    
     private:
      int * item;
      Element* next;
    };  // Missing semicolon
    
    #endif
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    8
    Thank you, thank you very much. Do you know where I could get info on using templates to allow me to pass any data type, as opposed to just int?

    Thanks again

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by thebudbottle
    Thank you, thank you very much. Do you know where I could get info on using templates to allow me to pass any data type, as opposed to just int?

    Thanks again

    Well, google should be able to help with sources on explaining templates. But... to start with:

    Code:
    template <typename T>
    class Element{
     public:
      Element(T* newItem);
      T* getItem();
      void setNext(Element*);
      Element * getNext();
    
     private:
      T * item;
      Element* next;
    };
    
    // Put code from CPP source file into header here and delete the souce file
    // Make changes to this source similar to changes made above
    One thing to note, with templates, you can't really have them separately as a header file and a source file (you can but you need a special compiler [Comeau(?) I think]). All the code must be in the header file as indicated by the note above.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. invalid types 'int[int]' for array subscript
    By kolistivra in forum C++ Programming
    Replies: 6
    Last Post: 12-11-2010, 12:57 PM
  2. Replies: 6
    Last Post: 08-23-2008, 01:16 PM
  3. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 10:16 AM