Classes

I am new to classes and I'm having trouble figuring out to to create the constructors for them.

Below is my class, and after that is my copy constructor (I have the default constructor already working, so I didn't include that here).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#pragma once
#include <iostream>

#define max_length 256

class xstring
{
     public:
          // Constructors
          xstring();
          xstring(const char *);

          // Overloaded operators
          xstring & operator = (const xstring &);
          friend std::ostream & operator << (std::ostream &, const xstring &);

     private:
          char      data[max_length + 1];
          int       length;
};



1
2
3
4
xstring::xstring (const xstring & list)
{
        //insert code here
}


I keep getting an error "error: definition of implicitly-declared ‘constexpr xstring::xstring(const xstring&)’
xstring::xstring (const xstring & list)"

I'm not sure what I'm doing wrong in this situation, but like I said, classes are new to me. I'm trying to follow examples that look like this, but with no help so far.
Your class definition declares xstring(const char *) but your implementation is for xstring(const xstring &)
Of course, I didn't notice that earlier.

I think I fixed it. I've built the very basic outline for each item, and I've tried to run it just to see what would happen, and it's giving me a segmentation fault. Is there something in this code that may cause that?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "xstring.h"

using namespace std;

//default constructor
xstring::xstring ()
{
}

//parameterized constructor
xstring::xstring (const char *list)
{
}

//overload copy operator
xstring & xstring::operator = (const xstring & list)
{
}

//overload << operator
std::ostream & operator << (std::ostream & out, const xstring & list)
{
}
enable warnings.
read the warnings that your compiler is giving you.
Last edited on
//overload copy operator
xstring & xstring::operator = (const xstring & list)

That's an assignment operator, not a copy constructor. Your class declaration should have both:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#pragma once
#include <iostream>

#define max_length 256

class xstring
{
     public:
          // Constructors
          xstring();
          xstring(const char *);
          xstring(const xstring &); // copy constructor

          // Overloaded operators
          xstring & operator = (const xstring &);
          friend std::ostream & operator << (std::ostream &, const xstring &);

     private:
          char      data[max_length + 1];
          int       length;
};
Is there something in this code that may cause that?
1
2
3
4
5
6
7
xstring & xstring::operator = (const xstring & list)
{
} // This implementation does not return what declaration promises

std::ostream & operator << (std::ostream & out, const xstring & list)
{
} // This implementation does not return what declaration promises 

Both operators should return a reference to object.
Neither has return statement.
Last edited on
Topic archived. No new replies allowed.