【问题标题】:what causes c++ std::string variable gives conflicting defenition when passing to class是什么导致 c++ std::string 变量在传递给类时给出冲突的定义
【发布时间】:2015-05-05 01:38:01
【问题描述】:

我有一个类,它有一个带字符串的构造函数; 当传入一个文字以创建一个新的瞬间时,它工作正常。 现在,当我创建一个要读取的字符串变量时,它会抛出一个错误,指出存在冲突的定义。该变量能够成功传递给非类函数。 代码如下:

/*********************
*
*
*draw a box around 
*words
*each word gets its 
*own line
*********************/
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <sstream>
    #include <vector>
    using namespace std;


class Box 
{
short width;// allows for an empty box to appear as two lines
short height;// makes an empty box a 2x2 box

string text;
//will only be constructed with a word
public:
Box(string& words): text(words)
{
    height = calc_height()+2;
    cout<<height -2<<endl<<text<<endl;
    width = calc_Max_length();
    cout<<width<<endl<<text;

}
private:
short calc_height()
{
    long total = text.length();
    string space = " ";
    short num_words =1; // calculated by the number of spaces, i know poor option but still hey itll mostly work assuming one word at min
    bool afterword = false;
    for(int i =0; i<text.length(); i++)
    {
        if(text[i] == ' ')
        {

            if(afterword)
            {
                num_words+=1;
                afterword=false;
            }
        }
        else
            {afterword=true;}
    //    cout<<i<<'\t'<<num_words<<endl;

    }
//    cout<<num_words;
    return num_words;

}
short calc_Max_length()
{

    short dist_between =0;
    short max = 0;
    short last = 0;
    bool afterword = false;
        for(int i =0; i<text.length(); i++)
    {
        if(text[i] == ' ')
        {
        //    dist_between = i-last;
            max = dist_between>max?dist_between:max;
            last = i;
            dist_between = 0;

        }
        else
            {
                dist_between++; 
            }
    //    cout<<i<<'\t'<<num_words<<endl;

    }
        max = dist_between>max?dist_between:max;

    return max;
}




};


void takes_string(string& s)
    {
    cout<<s;
    return;
    }


int main(void)
    {   
        string input = "crappy shit that sucks";
        takes_string(input);
    //    cin>>input;
        Box(input);

        return 0;
    }

【问题讨论】:

  • 真的吗?文字不应该起作用。参数应该是const string &amp;
  • EJP 是对的。非const 参数无法绑定到临时对象,因此无法将字符串文字传递给您的参数。如果你想接受字符串文字作为输入,你必须接受输入作为const 引用。
  • 您的实际错误信息是什么?
  • “冲突定义”并不表示参数类型不正确。它表明您对某事有多种定义。
  • @EJP 在我将构造函数参数更改为引用之前,它已使用文字。

标签: c++ string class constructor


【解决方案1】:
Box(input);

...等价于...

Box input;

也就是说,它试图创建一个类型为 Box 且标识符为 input 的变量,但已经有一个名为 inputstd::string,因此重定义错误。

您显然想要的 - 由 input 字符串构造的临时 Box - 应该用 C++11 编写 - 像这样......

Box{input};

FWIW,一个好的编译器应该在错误消息中清楚地说明这一点,例如来自 coliru.stackedcrooked.com 上的 GCC:

main.cpp: In function 'int main()':
main.cpp:99:18: error: conflicting declaration 'Box input'
         Box(input);
                  ^
main.cpp:96:16: note: previous declaration as 'std::string input'
         string input = "crappy shit that sucks";
                ^

【讨论】:

  • Box b(input); 用输入构造一个变量。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-06-29
  • 2011-04-18
  • 2019-07-11
  • 2015-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多