Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过在c+中赋值初始化自己的字符串类+; 我在C++中编写自己的string类 String EX/COD>(不用担心,只是为了练习),但是我不能通过给它分配一个字符串来创建类的实例: StringEx string1 = StringEx("test"); // works fine StringEx string2 = "test"; // doesn't work string string3 = "test"; string1 = string3; // also works fine_C++_String_Class - Fatal编程技术网

通过在c+中赋值初始化自己的字符串类+; 我在C++中编写自己的string类 String EX/COD>(不用担心,只是为了练习),但是我不能通过给它分配一个字符串来创建类的实例: StringEx string1 = StringEx("test"); // works fine StringEx string2 = "test"; // doesn't work string string3 = "test"; string1 = string3; // also works fine

通过在c+中赋值初始化自己的字符串类+; 我在C++中编写自己的string类 String EX/COD>(不用担心,只是为了练习),但是我不能通过给它分配一个字符串来创建类的实例: StringEx string1 = StringEx("test"); // works fine StringEx string2 = "test"; // doesn't work string string3 = "test"; string1 = string3; // also works fine,c++,string,class,C++,String,Class,我重载了赋值运算符,因此它可以处理std::string,但我必须首先创建StringEx的对象 如何通过为StringEx指定字符串来创建新对象?是否可以获得C++作为“我的代码< String EX/代码>类< /P>的对象处理每个字符串”? 这是我的StringEx.h,现在可以使用了 #ifndef STRINGEX_H #define STRINGEX_H #include <iostream> #include <string> #include <

我重载了赋值运算符,因此它可以处理
std::string
,但我必须首先创建
StringEx
的对象

如何通过为
StringEx
指定字符串来创建新对象?是否可以获得C++作为“我的代码< String EX/代码>类< /P>的对象处理每个<代码>字符串”<代码>? 这是我的
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*);  // 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
\ifndef STRINGEX\u H
#定义STRINGEX_H
#包括
#包括
#包括
使用名称空间std//现在简单化
StringEx类
{
私人:
矢量文本;
公众:
StringEx();
StringEx(string);
StringEx(const char*);//必须添加此
StringEx(载体);
int size()常量;
int length()常量;
(int)常量处的字符;
无效附加(常量字符串&);
无效附加(常量StringEx&);
void append(const char*);//必须添加此
StringEx运算符+(常量字符串&);
StringEx运算符+(常量StringEx&);
StringEx运算符+(const char*);//也必须添加此运算符
StringEx运算符=(常量字符串&);
StringEx运算符=(常量StringEx&);
StringEx运算符=(const char*);//也必须添加此运算符
StringEx运算符+=(常量字符串&);
StringEx运算符+=(常量StringEx&);
StringEx运算符+=(const char*);//也必须添加此运算符

friend ostream&operatorTLDR:如果需要两个“强制转换”,编译器将不会自动解决。在这种情况下,
const char*
to
std::string
to
StringEx


StringEx string1=StringEx(“test”);//工作正常
>这会显式调用构造函数,因此只需执行一次转换:
const char*
std::string

StringEx-string2=“test”//不起作用
>另一方面,这一点不清楚。您想将
const-char*
强制转换为
std::string
并使用
StringEx(const-std::string&)
构造函数还是使用其他中间类

您需要的是另一个接受
constchar*
作为参数的构造函数

顺便说一句:


我假设您有一个非显式的表单构造函数

StringEx (const std::string&);
或类似的

字符串文本不是类型
std::String
,“test”
是类型
const char[5]
std::String
有一个接受
const char*
的非显式构造函数,因此您的两个调用如下所示(不考虑复制省略):

解决此问题的一个简单方法是将一个构造函数添加到
StringEx
,该构造函数采用
const char*

StringEx (const char*);
或者,您可以只使用直接初始化:

//only one conversion needed
StringEx string2 ("test");
一些精度:

StringEx string1 = StringEx("test"); // works fine
这将使用复制构造函数,即
StringEx(const-StringEx&other);

这将尝试使用具有以下签名的构造函数:
StringEx(const char*str);

最后,这两行:

string string3 = "test";
string1 = string3; // also works fine
从标准库定义的
const char*
创建一个
std::string
,然后为您似乎已正确定义的
std::string
使用重载的复制分配运算符,即
StringEx&operator=(const std::string&other)

这里的关键点是这句话:

Type myVar = something;
不是赋值,而是使用构造函数而不是复制赋值运算符的变量的声明和初始化


在您的例子中,您只是缺少一个构造函数,该构造函数将
常量char*
作为参数。

这称为初始化(更准确地说,是复制初始化),而不是赋值。赋值是指对象之前已经构造好的时间(如上一个示例中)。奇怪的是:这段代码用VS2015编译得很好。您已经得到了一些答案,但您确实应该显示类的构造函数、复制构造函数和复制赋值的声明。@SimonKraemer MSVC从来都不是标准遵从性的典范。@LPrulzcrossover您的编辑显示了导致错误,还是现在的代码> SrimeX.H./Cube?请编辑你的问题并解释你正在显示的内容。原始代码可以用MSVC14/VS2015编译,并且工作得很好。尽管IDE显示了一个错误。谢谢,现在工作!是否可以获得C++的每一个<代码> const char */Cube >作为<代码> StrugEX <代码>?也就是说,
cout@LPrulzcrossover你不能含蓄地(好吧,也许你可以用一种我不知道的非常丑陋的方式)。无论如何,你为什么要这样做?这行可以为两个
const char*
适当重载
operator+
,例如返回
std::string
。甚至可以为两个
const char*
重载
operator+
?考虑到答案似乎是否定的,或者你会怎么做?嘘之前已经检查过了,实际上是不可能的。在C.135.6中的C++标准规定,在重载二进制运算符时,至少有一个参数类型必须是类、类的引用、枚举或枚举的引用。
StringEx string2 = "test"; // doesn't work
string string3 = "test";
string1 = string3; // also works fine
Type myVar = something;