I do not know how to work with:String::String(char const*)

I get this error when compiling my program:
undefined reference to `String::String(char const*)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
 #include<cstring>
#include<iostream>
#include<cassert>
#include<iomanip>
#include<bits/stdc++.h>

using namespace std;

class newString
{
    friend ostream& operator<<(ostream& , const newString&);
public:
    const newString& operator+(const newString&);
    newString(const char*);
    newString(const newString&); //copy constructor
    newString();
private:
    char* strPtr;
    int strLength;
};
newString::newString()  //default constructor
{
    strLength = 0;
    strPtr = new char[1];
    assert(strPtr != NULL);
    strcpy(strPtr, "");
}
newString::newString(const newString& rightStr)
{
    strLength = rightStr.strLength;
    strPtr = new char[strLength + 1];
    assert(strPtr != NULL);
    strcpy(strPtr, rightStr.strPtr);
}
ostream& operator<<(ostream& osObject, const newString& str)
{
    osObject<str.strPtr;
    return osObject;
}
const newString& newString::operator+(const newString& )
{
    newString s1, s2;
    cout << "\nConcatenation: " << s1 + s2;
}

int main()
{
    newString s3;
    newString s1 = "Hello";
    newString s2 = " there";
    s3 = s1 + s2;
}
 


any help would be appreciated
That message makes no sense. There is no class called String anywhere in your code.

Are you sure you've posted the right code?

EDIT: I will note that you've declared a constructor newString(const char*) at line 14, but haven't defined it anywhere.
Last edited on
the class is called newString. so that line is :
newString::newString(char const*)
my question is that I do not know how to define constructor.
Not yet good at working with pointers
presumably the constructor would new up your internal char* variable to some size and then strcpy the incoming parameter into it, and set anything else that matters, perhaps its length?

that + operator is recursive and looks like it may do something odd.
Last edited on
the class is called newString. so that line is :
newString::newString(char const*)
my question is that I do not know how to define constructor.

So basically, you got an error message that complained about newString::newString(char const*) not being defined, and you knew already that you hadn't defined it, but... you decided to come and post in here about it anyway? Why? What on earth did you think needed explaining about that error, when you already knew you hadn't defined that function?

I don't understand why you claim that you don't know how to define constructors, when you already define two of them in the code you've posted.

Are you trolling?

Why is your no argument constructor allocating memory for the pointer instead of just assigning the pointer to a nullptr? A nullptr should probably be the expected occurrence if the string is empty.

Also you should get used to use initialization lists with your constructors.

1
2
3
4
newString::newString()  : strLength(0), strPtr(nullptr)
{
   // Blank body.
}


I have defined that constructor as:
[code]

newString::newString(const char *str)
{
strLength = strlen(str);
strPtr = new char[strLength + 1];
//allocate memory to store the char string
assert(strPtr != NULL);
strcpy(strPtr,"");
}
[/copy]

The program bombs out when I ran it and I got:


Process returned 255 (0xFF) execution time : 17.271 s
Press any key to continue.

help please
code should have been:
1
2
3
4
5
6
7
8
9

newString::newString(const char *str)
{
strLength = strlen(str);
strPtr = new char[strLength + 1];
//allocate memory to store the char string
assert(strPtr != NULL);
strcpy(strPtr,"");
}
Your constructors look fine. You have some other problems though.

Line 11: Your << operator is private. Not very useful.

Line 37: You're using the < operator.

Lines 40-43: As jonin pointed out your + operator is recursive. This is probably the cause of your crash when you ran out of stack space. Line 40: You don't name the argument. How are you going to append something that you don't name? Line 42: These are empty strings. Line 43: You're trying to concatenate the two empty strings by recursively calling your + operator.

40
41
42
43
44
45
46
47
48
49
 
const newString& newString::operator+(const newString & rhs)
{   newString rslt;
    
    rslt.strLength = this->strLength + rhs.strLength;
    rslt.strPtr = new char[this->strLength + 1];
    strcpy(rslt.strPtr, this->strPtr);
    strcat(rslt.strPtr, rhs.strPtr);
    cout << "\nConcatenation: " << rslt << endl;
}


BTW, you have no destructor that cleans up allocated memory. Consequently, you have a memory leak.
Last edited on
Topic archived. No new replies allowed.