-4

Note: I have searched thoroughly on SO and the solutions posted for other's with similar questions are not working for me here.

I am writing my own custom 'string-like' class in C++, and am encoutering the following errors when compiling:

./PyString.h:8:11: error: out-of-line declaration of 'PyString' does not match any declaration in 'PyString' PyString::PyString (char*); ^

./PyString.h:9:11: error: definition of implicitly declared destructor PyString::~PyString (void);

pystring.cpp:4:7: error: redefinition of 'PyString' class PyString {

As for the first and second errors, moving around the destructor into the class definition itself in the cpp file did not work.

As for the third error, I can't seem to fix it - I'm not redefining the class!

Here is pystring.h:

#ifndef PYSTRING_INCLUDED
#define PYSTRING_INCLUDED

class PyString {
    char* string;
};

PyString::PyString (char*);
PyString::~PyString (void);

#endif

Here is pystring.cpp:

#include "PyString.h"
#define NULL 0

class PyString {
    char* string = NULL;
  public:
    PyString(char inString) {
      string = new char[inString];
    };

    ~PyString(void) {
      delete string;
    };
};

For reference, here is the compile output as a screenshot: Compiler output screenshot

Any help is greatly appreciated.

techydesigner
  • 1,681
  • 2
  • 19
  • 28
  • 7
    I think you should get [a good beginners book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and start over, because it's not much in your shown source that is correct. – Some programmer dude Oct 11 '16 at 07:13
  • 1
    Look up *any* example of a class defined in a header with its members defined in a `.cpp` file. – juanchopanza Oct 11 '16 at 07:16

2 Answers2

2

You're defining your class PyString in your header AND in your cpp file, and also, a function definition doesn't need a ; at it's end.
And... your function prototypes needs to be in your class declaration in your header :

pystring.h

class PyString {
public: //ALWAYS indicate what is public/private/protected in your class
    PyString (char* inString);
    ~PyString (); // Don't put void when there's no parameter

private: // All attributes are private
    char* string;
};

pystring.cpp

#include "PyString.h"

PyString::PyString(char* inString) {
    string = inString; // Avoid using new unless you're forced to
}

PyString::~PyString() {
}
Treycos
  • 7,373
  • 3
  • 24
  • 47
  • 1
    `PyString::` should not be present inside the class definition (it's an error but some compilers let it pass). Also your suggestion for "pystring.c" is completely wrong – M.M Oct 11 '16 at 07:29
  • Damn, i'm making a lot of mistakes today... thanks – Treycos Oct 11 '16 at 07:30
  • I copied both the snippets into separate files, but is says that `PyString::PyString` is an extra qualification. In addition, it still says that there is a redeclaration. – techydesigner Oct 11 '16 at 07:32
  • It won't anymore, idk why people upvoted my wrong answer before this edit.... – Treycos Oct 11 '16 at 07:34
  • @Treycos: And I was *seriously* about to launch my IDE to see if, in total bewilderment, if your previous answer compiled ;-) My upvote is a symptom of my elation in being reassured that I can still do this. – Bathsheba Oct 11 '16 at 07:35
  • Oh god, poor complier, thats rude – Treycos Oct 11 '16 at 07:36
  • Aha, it worked! Thank you. – techydesigner Oct 11 '16 at 07:38
0

Oh yes you are! pystring.h contains

class PyString {
    char* string;
};

which is a class declaration. The declarations PyString::PyString (char*); and PyString::~PyString (void); need to be inside that declaration.

But you have something similar in pystring.cpp, specifying additional functions, and defining some of them. That's what your compiler is telling you.

Normally, you fully define a class in a header (i.e. all members, and the declaration of the member functions), and implement the member functions of that class in a source file.

The moral of the story here: you can't really learn C++ by trial and error. Get a good book!

Bathsheba
  • 231,907
  • 34
  • 361
  • 483