I'm new to c++.I made a simple program that name members of class based on your input.
But for some reason, compiler shows this error - 'str': is not a member of 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>', I have trouble understanding what it means.
Please help me out
here is my code -
#include <iostream>
#include <string>
#include <vector>
class mystring
{
private:
std::string *str;
public:
//constructors
mystring();
mystring(const std::string &strthing);
~mystring();
//methods
void display() const;
};
mystring::mystring() //defalt constructor
:str(nullptr)
{}
mystring::mystring(const std::string& strthing) //copy constructor
:str(nullptr)
{
delete str;
str = new std::string;
strcpy(this->str, strthing.str);
std::cout << "overloaded\n";
}
mystring::~mystring() //destructor
{
delete [] str;
}
void mystring::display() const //display func
{
std::cout << *str;
}
int main()
{
mystring thing;
mystring object{ "samurai" };
object.display();
}
I think something is wrong with the strcpy() function in overloaded constructor.
Thanks
There is nothing wrong with strcpy, but with how you use it. std::strings can be copied with their operator=:
std::string a;
std::string b;
a = b; // copy b to a
strcpy on the other hand is for c-strings, which a std::string is not:
char* strcpy( char* dest, const char* src );
It is unclear why you have a member of type pointer to std::string. You should either use a std::string (no pointer), or if this is an exercise to write your own string class (not an easy one!) then you should probably store the data in an array of chars.
The error you get is about strthing.str. Here strthing is a std::string which has no str member. If that constructor is supposed to be a copy constructor it should take a const mystring& as parameter not a const std::string&.
This is just a typo.
mystring::mystring(const std::string& strthing)
Should be
mystring::mystring(const mystring& strthing)
You don't use strcpy to copy std::string objects. Here's your constructor rewritten correctly (it's not a copy constructor because it doesn't copy mystring objects)
mystring::mystring(const std::string& strthing) // constructor from std::string
: str(new std::string(strthing)) // allocate new string by copying from strthing
{
std::cout << "overloaded\n";
}
Because your class allocates memory (not sure why it does but it does) you do actually need to write a genuine copy constructor and assignment operator
mystring::mystring(const mystring& strthing) // copy constructor
mystring& mystring::operator=(const mystring& strthing) // assignment operator
But I'll leave that to you.
Ther is a C++ trap between C++ type std::string and C type 'char*'.
std::string can convert to char* using std::string.c_str().
And strcpy takes char* and char const * as parameters.
In moderen C++ practice, use std::string usually, std::string * is not recommended to use.
If you want to use strcpy with std::string, you have to do the conversion between char * and std::string. Below is a simple Example:
std::string dest;
std::string source = "Hello, World!";
char temp[40];
strcpy(temp, source.c_str());
dest = temp;
Related
Here is my code:
class Mystring
{
private:
unsigned int size;
shared_ptr<char>message=make_shared<char>();
public:
Mystring(const char* input):size(strlen(input)),message(make_shared<char>(size+1))
{cout<<"Created"<<endl;memcpy(this->message,input,size+1);}
Mystring(const Mystring& other):size(other.size),message(make_shared<char>(size+1))
{cout<<"Created"<<endl;memcpy(this->message,other.message,size+1);}
~Mystring(){cout<<"Deleted"<<endl;}
friend ostream& operator<<(ostream& Cout,const Mystring& mystring)
{Cout<<mystring.message<<endl;return Cout;}
char& operator[](const unsigned int index)
{return this->message[index];}
};
I want to use smart pointers and operator [] properly. How can I solve these problems:
no suitable conversion function from "std::shared_ptr<char>" to "void *" exists
no operator "[]" matches these operands
So lots of problems with this code
You have a shared char array, so the type is shared_ptr<char[]> not shared_ptr<char>. This also means you cannot use make_shared, since it doesn't work with arrays.
If you want to access the pointer to the array use get().
Your copy constructor allocates a new array, which is a bit weird since why are you using shared_ptr, if you don't want to share the array? On the other hand the default assignment operator will share the array, so you have a strange situation where sometimes when you copy Mystring you will share the array, and sometimes you won't.
Various other minor issues
Here's a version that works
class Mystring
{
private:
size_t size;
shared_ptr<char[]> message;
public:
Mystring(const char* input) : size(strlen(input)), message(new char[size+1])
{
cout<<"Created"<<endl;
memcpy(message.get(), input, size+1);
}
Mystring(const Mystring& other): size(other.size), message(other.message)
{
cout<<"Created"<<endl;
}
~Mystring()
{
cout<<"Deleted"<<endl;
}
friend ostream& operator<<(ostream& Cout,const Mystring& mystring)
{
Cout<<mystring.message.get()<<endl;return Cout;
}
char& operator[](size_t index)
{
return message.get()[index];
}
};
I changed the copy constructor so that it does share the underlying array. You can change it back if you wish. However is you want a Mystring which does not share it's array with other Mystring objects then it would make more sense to use unique_ptr<char[]> instead of shared_ptr.
I'm having some issues compiling the code I wrote which has a custom class with an overloaded =.
rnumber.hpp:
#include <string>
class rnumber {
public:
std::string number;
// I added this constructor in an edit
rnumber(std::string s) { number = s; }
void operator=(std::string s) { number = s; }
bool operator==(std::string s) { return number == s; }
friend std::ostream &operator<<(std::ostream &os, const rnumber n);
};
std::ostream &operator<<(std::ostream &os, const rnumber n) {
os << n.number;
return os;
}
main.cpp:
#include "rnumber.hpp"
#include <iostream>
#include <string>
int main() {
rnumber a = "123";
}
For some reason, this does not compile with the error conversion from ‘const char [4]’ to non-scalar type ‘rnumber’ requested. This shouldn't be a problem, because rnumber has an overload for =. Why am I getting this error?
EDIT: Even after adding a constructor, it doesn't work.
rnumber a = "123"; tries to initialize a with a const char*, but there is no constructor taking a const char*.
You need to implement this constructor as well:
rnumber(const char *s) { number = s; }
There are other possibilities, mentioned on other answer and comments.
The problem is that rnumber a = "123"; is initialization and not assignment so that the copy assignment operator= cannot be used.
There are 2 ways to solve this as shown below:
Method 1
Use the converting constructor rnumber::rnumber(std::string)
int main() {
rnumber a("123"); //this uses the converting ctor
}
Method 2
Create the object and then do the assignment:
int main() {
rnumber a("somestring"); //creates the object using converting ctor
a = "123"; //uses assignment operator
}
"abc" is not a std::string literal, it is a string literal, in particular it is a const char[4]. There is an implicit conversion from const char[4] to std::string, by one of it's constructors, and an implicit conversion from std::string to rnumber by the constructor you edited in, but you only get one implicit constructor per initialisation.
What you can do is explicitly construct the rnumber, and the std::string parameter can be implicitly constructed.
int main() {
rnumber a { "123" };
}
The problem's that I was using the wrong constructor.
rnumber(std::string s) { number = s; } is an std::string constructor, where the assignment rnumber a = "123"; is assigning a const char* to a.
Add another constructor rnumber(const char *s) { number = s; } to the header.
I'm writing my own string class StringEx in c++ (don't worry, just for exercise) but I'm failing at creating an instance of my class by assigning a string to it:
StringEx string1 = StringEx("test"); // works fine
StringEx string2 = "test"; // doesn't work
string string3 = "test";
string1 = string3; // also works fine
I overloaded the assignment operator so it can handle with std::string but I have to create an object of StringEx first.
How can I create a new object of StringEx by assigning a string to it? Is it even possible to get c++ handling every "string" as an object of my StringEx class?
This is my StringEx.h which works now
#ifndef STRINGEX_H
#define STRINGEX_H
#include <iostream>
#include <string>
#include <vector>
using namespace std; //simplyfying for now
class StringEx
{
private:
vector<char> text;
public:
StringEx();
StringEx(string);
StringEx(const char*); // had to add this
StringEx(vector<char>);
int size() const;
int length() const;
char at(int) const;
void append(const string&);
void append(const StringEx&);
void append(const char*); // had to add this
StringEx operator+(const string&);
StringEx operator+(const StringEx&);
StringEx operator+(const char*); // had to add this too
StringEx operator=(const string&);
StringEx operator=(const StringEx&);
StringEx operator=(const char*); // had to add this too
StringEx operator+=(const string&);
StringEx operator+=(const StringEx&);
StringEx operator+=(const char*); // had to add this too
friend ostream& operator<<(ostream&, const StringEx&);
};
#endif // STRINGEX_H
A few precisions:
StringEx string1 = StringEx("test"); // works fine
This use the copy-constructor, i.e. StringEx(const StringEx& other);
StringEx string2 = "test"; // doesn't work
This tries to use a constructor with the following signature: StringEx(const char* str);
Finally, those two lines:
string string3 = "test";
string1 = string3; // also works fine
create an std::string from a const char*, which the standard library defines, and then use the copy-assignment operator overloaded for std::string that you seem to have defined correctly, i.e. StringEx& operator=(const std::string& other).
The key point here is that this statement:
Type myVar = something;
is not an assignement, it's an declaration and initialisation of a variable which uses a constructor, not the copy-assignment operator.
In your case, you're just missing a constructor that takes a const char* as parameter.
I assume that you have a non-explicit constructor of the form
StringEx (const std::string&);
or similar.
String literals are not of type std::string. "test" is of type const char[5]. std::string has a non-explicit constructor accepting a const char*, so your two calls look like this (not taking copy elision into account):
//implicitly convert "test" to temporary std::string
//construct temporary StringEx with temporary std::string
//copy-construct StringEx with temporary
StringEx string1 = StringEx("test");
//no StringEx constructor taking const char*
//two conversions necessary to make "test" into a StringEx
//invalid
StringEx string2 = "test";
An easy fix for this is to add a constructor taking a const char* to StringEx:
StringEx (const char*);
Alternatively you could just use direct initialization:
//only one conversion needed
StringEx string2 ("test");
TLDR: The compiler won't autoresolve if 2 "casts" are necessary. In this case const char* to std::string to StringEx
StringEx string1 = StringEx("test"); // works fine > This calls the constructor explicitly and therefore only one conversion must be done: const char* to std::string.
StringEx string2 = "test"; // doesn't work > This on the other hand is not clear. Do you want to cast const char* to std::string and use the StringEx(const std::string&)constructor or use some other intermediate class?
What you need is another constructor accepting const char* as parameter.
BTW:
StringEx string4 = std::string("test"); //Also works
I have a class defined:
#ifndef _STRINGCLASS_H
#define _STRINGCLASS_H
using namespace std;
#include <iostream>
#include <vector>
class String {
protected:
int length;
vector<string> buf;
public:
String();
String(const char* input);
String(char input);
String(int input);
String(const String& input);
String(char input, int input2);
String& operator=(const String& input);
};
#endif
and am trying to overload the assignment operator by such:
String& operator=(const String& input) {
buf = input.buf;
length = input.length;
return *this;
}
and I get the error code that buf is protected and length is protected. I'm not sure what I am missing. How can I properly overload the assignment operator with vectors and ints?
You do not need to provide any special member functions for your class, because the compiler synthesized ones will do the right thing in this case. The best option is to remove the assignment operator and copy constructor from your class definition.
class String
{
protected:
int length;
vector<string> buf;
public:
String();
String(const char* input);
String(char input);
String(int input);
String(char input, int input2);
};
You need to define the implementation as part of the class. You are missing the class specifier:
// vvvvvvvv
String& String::operator=(const String& input) {
buf = input.buf;
length = input.length;
return *this;
}
As written, you are defining a free operator overload (not bound to a class), and it's actually invalid to declare a free assignment operator overload anyway.
From the perspective of a free operator overload that isn't a member of String, buf and length are indeed inaccessible because they are private.
I just need a little help on this assignment. I have to redefine operators to work with strings. I'm starting with the == operator and I have it declared in my header file, however when I go to define the function in my cpp file, it says it's incompatible with the declared function. It's probably a stupid mistake, I just don't understand this sometimes.
string.h header file
#pragma once
#include <iostream>
#include <string>
using namespace std;
#define NOT_FOUND -1
// C++ String class that encapsulates an ASCII C-string
class String
{
public:
// Default constructor
String();
// MUST HAVE: Copy-constructor that performs deep copy
String(const String& source);
// Init-constructor to initialize this String with a C-string
String(const char* text);
// Init constructor, allocates this String to hold the size characters
String(int size);
// Destructor
~String();
bool& compareTo(const String& cmp1);
// Assignment operator to perform deep copy
String& operator = (const String& source);
// Assignment operator to assign a C-string to this String
String& operator = (const char* text);
// Returns a reference to a single character from this String
char& operator [] (int index) const;
// Comparison operators
bool operator == (const String& compareTo) const;
string.cpp file
#include "string.h"
#include <string>
#include <sstream>
using namespace std;
// Default constructor
String::String()
{
Text = NULL;
}
// MUST HAVE: Copy-constructor that performs deep copy
String::String(const String& source)
{
Text = NULL;
// Call the assignment operator to perform deep copy
*this = source;
}
// Init-constructor to initialize this String with a C-string
String::String(const char* text)
{
Text = NULL;
// Call the assignment operator to perform deep copy
*this = text;
}
// Init constructor, allocates this String to hold the size characters
String::String(int size)
{
Text = new char[size];
}
// Destructor
String::~String()
{
delete[] Text;
}
// Assignment operator to perform deep copy
String& String::operator = (const String& source)
{
// Call the other assigment operator to perform deep copy
*this = source.Text;
return *this;
}
// Assignment operator to assign a C-string to this String
String& String::operator = (const char* text)
{
// Ddispose of old Text
delete[] Text;
// +1 accounts for NULL-terminator
int trueLength = GetLength(text) + 1;
// Dynamically allocate characters on heap
Text = new char[trueLength];
// Copy all characters from source to Text; +1 accounts for NULL-terminator
for ( int i = 0; i < trueLength; i++ )
Text[i] = text[i];
return *this;
}
***bool& String::operator ==(string cmp2)***
{
};
Your compareTo declaration has const while definition has no const, which means they have definition has different signature with declaration:
bool& compareTo(const String& cmp1);
^^^
bool& String::compareTo(string cmp2)
{
};
BTW, why does your compareTo return bool& ?
Also should avoid using namespace std; in any header files. see why-is-using-namespace-std-considered-a-bad-practice-in-c