Skip to main content
added 122 characters in body
Source Link
SimonH
  • 1.5k
  • 16
  • 37

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?

As requested, hereThis 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

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?

As requested, here is my StringEx.h

#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*);
    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*);

    StringEx operator+(const string&);
    StringEx operator+(const StringEx&);
    StringEx operator+(const char*);

    StringEx operator=(const string&);
    StringEx operator=(const StringEx&);
    StringEx operator=(const char*);

    StringEx operator+=(const string&);
    StringEx operator+=(const StringEx&);
    StringEx operator+=(const char*);

    friend ostream& operator<<(ostream&, const StringEx&);
};

#endif // STRINGEX_H

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
added 158 characters in body
Source Link
SimonH
  • 1.5k
  • 16
  • 37

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?

As requested, here is my StringEx.h

#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*);
    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*);

    StringEx operator+(const string&);
    StringEx operator+(const StringEx&);
    StringEx operator+(const char*);

    StringEx operator=(const string&);
    StringEx operator=(const StringEx&);
    StringEx operator=(const char*);

    StringEx operator+=(const string&);
    StringEx operator+=(const StringEx&);
    StringEx operator+=(const char*);

    friend ostream& operator<<(ostream&, const StringEx&);
};

#endif // STRINGEX_H

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?

As requested, here is my StringEx.h

#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*);
    StringEx(vector<char>);

    int size() const;
    int length() const;
    char at(int) const;

    void append(const string&);
    void append(const StringEx&);

    StringEx operator+(const string&);
    StringEx operator+(const StringEx&);

    StringEx operator=(const string&);
    StringEx operator=(const StringEx&);

    StringEx operator+=(const string&);
    StringEx operator+=(const StringEx&);

    friend ostream& operator<<(ostream&, const StringEx&);
};

#endif // STRINGEX_H

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?

As requested, here is my StringEx.h

#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*);
    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*);

    StringEx operator+(const string&);
    StringEx operator+(const StringEx&);
    StringEx operator+(const char*);

    StringEx operator=(const string&);
    StringEx operator=(const StringEx&);
    StringEx operator=(const char*);

    StringEx operator+=(const string&);
    StringEx operator+=(const StringEx&);
    StringEx operator+=(const char*);

    friend ostream& operator<<(ostream&, const StringEx&);
};

#endif // STRINGEX_H
added the StringEx.h
Source Link
SimonH
  • 1.5k
  • 16
  • 37

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?

As requested, here is my StringEx.h

#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*);
    StringEx(vector<char>);

    int size() const;
    int length() const;
    char at(int) const;

    void append(const string&);
    void append(const StringEx&);

    StringEx operator+(const string&);
    StringEx operator+(const StringEx&);

    StringEx operator=(const string&);
    StringEx operator=(const StringEx&);

    StringEx operator+=(const string&);
    StringEx operator+=(const StringEx&);

    friend ostream& operator<<(ostream&, const StringEx&);
};

#endif // STRINGEX_H

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?

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?

As requested, here is my StringEx.h

#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*);
    StringEx(vector<char>);

    int size() const;
    int length() const;
    char at(int) const;

    void append(const string&);
    void append(const StringEx&);

    StringEx operator+(const string&);
    StringEx operator+(const StringEx&);

    StringEx operator=(const string&);
    StringEx operator=(const StringEx&);

    StringEx operator+=(const string&);
    StringEx operator+=(const StringEx&);

    friend ostream& operator<<(ostream&, const StringEx&);
};

#endif // STRINGEX_H
added 125 characters in body
Source Link
SimonH
  • 1.5k
  • 16
  • 37
Loading
Source Link
SimonH
  • 1.5k
  • 16
  • 37
Loading