【问题标题】:error: no matching function for call to with constructor initialization错误:没有匹配的函数调用构造函数初始化
【发布时间】:2020-04-02 08:33:46
【问题描述】:

d.cpp 文件:

#include "file.hpp"

#include <iostream>
using namespace std;

int main() {
        ns::T t ("ssssss");
}

文件.hpp

using namespace std;


namespace ns{

    struct T{

        T(string s);

    };
    };

为什么会出现这个错误?:

file.hpp:8:20: error: 's' T(string s) 之前的预期')';

d.cpp:在函数“int main()”中:d.cpp:14:26:错误:没有匹配 调用‘ns::T::T(const char [6])’ns::T t ("ssssss")的函数; 在 d.cpp:8 中包含的文件中:file.hpp:6:12: 注意:候选人: ‘constexpr ns::T::T()’ struct T{

【问题讨论】:

  • 你的#include &lt;string&gt;呢?
  • 每当您遇到此类小文件的问题时,不要使用#include,只需复制粘贴代码并让您的 IDE 为您自动格式化,100% 您会立即看到问题。

标签: c++ constructor


【解决方案1】:

你需要包含“字符串”:

#include <string>
using namespace std;

namespace ns{

  struct T{
      T(string s);
  };
}

【讨论】:

  • ...并且在 file.hpp 的末尾缺少 }
  • 添加了字符串包含和右括号,但现在我得到了新错误“未定义对 `ns::T::T(std::__cxx11::basic_string, std::allocator >)'"
  • @Sergey 您的 file.cpp 是什么样的? T的构造函数你实现了吗?
  • @SergeyArenzon 您需要实施 T::T(string)。到目前为止,您只声明了它。
猜你喜欢
  • 1970-01-01
  • 2015-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-17
相关资源
最近更新 更多