1

I know there are a lot of questions about this, but I've checked my program for common mistakes, and I can't seem to find any.

I'm getting these errors for the first line of my copy constructor in a file strlist.cpp:

ISO C++ forbids declaration of ‘Strlist’ with no type [-fpermissive] no ‘int StrList::Strlist(const StrList&)’ member function declared in class ‘StrList’

Here is that section:

 /* copy constructor */
 40 StrList::Strlist(const StrList& rhs)
 41 {
 42    intitList(&list);
 43    Struct Node *current = (rhs.list).head;
 44    while(current != NULL){
 45       AddFront(*(const MyString *)current->data);
 46       current = current->next;
 47    }
 48    reverse();
 49 }

Here is the copy constructor in my header file:

  5 #ifndef __STRLIST_H__
  6 #define __STRLIST_H__
 11 
 12 #include "mystring.h"
 13 #include "stdio.h"
 14 #include "stdlib.h"

 20 
 21 extern "C" {
 22 #include "mylist.h"
 23 }
 24 
 25 class StrList {
 26 
 27     public:
(......................)
 40 
 41         /*copy constructor */
 42         StrList(const StrList& rhs);
(..................)
105 };
106 
107 #endif

I included strlist.h in strlist.cpp, so I can't figure out what is wrong with this.

Thanks!!

user146303
  • 437
  • 1
  • 7
  • 20
  • 4
    C++ is case-sensitive. You're also using a [reserved identifier](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). – chris May 05 '14 at 22:30
  • 1
    `Struct Node *current = (rhs.list).head;` Are you sure that `Struct` should be capitalised? – Brandon May 05 '14 at 22:33
  • 1
    Even lowercase, it serves no purpose being there. – chris May 05 '14 at 22:34
  • 1
    @chris I think he's trying to make it C compatible, but classes won't help with that :L – RamblingMad May 05 '14 at 22:36

2 Answers2

3

ISO C++ forbids declaration of ‘Strlist’ with no type

StrList::Strlist(const StrList& rhs)
//          ^    

You should be extra-careful when reading error messages, the compiler is trying to help you!

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
  • 1
    "the compiler is trying to help you" -- the error message is kind of strange as it does not say the StrList class does not have a class member called Strlist. I think Visual C++ used to give such error messages which are more helpful. – thor May 05 '14 at 22:35
  • It says: `no int StrList::Strlist(const StrList&)’ member function declared in class ‘StrList’` Which is a start.. It could be worse.. For example: `template` errors. The compiler shows you who's the boss ;) – Brandon May 05 '14 at 22:40
  • @TingL: The language is complex and the compiler will fail to give you a precise description, which is more of a reason to carefully read what it is telling you. In this case, from user code `Strlist` and the fact that there is no `StrList::Strlist` member function is quite helpful. Is it not? Does it not *say that the `StrList` class does not have a class member called `Strlist`*? – David Rodríguez - dribeas May 05 '14 at 22:47
  • @DavidRodríguez-dribeas Yes, we all recognize the complexity of the language, especially it's c++ we are talking about. All I am saying is that some version of c++ compiler used to give better error messages than this. Of course, we can infer/compute from what we have, and sympathize why the computer didn't do this for us. But the compile indeed was better, and say what you think it meant to say upfront. – thor May 05 '14 at 23:19
2

Simply change StrList::Strlist to StrList::StrList as the first way would not be referring to the constructor (wrong case on the List part)

RamblingMad
  • 5,332
  • 2
  • 24
  • 48