Use of class template 'list' requires template arguments; argument deduction not allowed in function return type
时间: 2024-03-01 09:55:26 浏览: 36
这个编译错误通常是因为在函数的返回类型中使用了模板,而模板参数无法从函数返回值中推断出来。
例如,下面这个函数就会出现上述错误:
```C++
std::list<int> get_list() {
std::list<int> my_list = {1, 2, 3, 4, 5};
return my_list;
}
```
在这个函数中,我们试图返回一个`std::list`类型的对象,但是编译器无法从返回值中推断出`std::list`的模板参数类型`int`,因此会报上述错误。
解决这个问题的方法是,显式地指定返回类型的模板参数类型,例如:
```C++
std::list<int> get_list() {
std::list<int> my_list = {1, 2, 3, 4, 5};
return my_list;
}
```
这样就可以明确指定返回类型的模板参数类型,避免编译错误。
相关问题
G:\Vmware\shared_folders\QT\day02\Simple_calculator\mainwindow.cpp:43: error: use of class template 'stack' requires template arguments
这个错误是因为在使用 `std::stack` 类时,需要指定堆栈中存储的数据类型作为模板参数。在第43行的代码中,您没有提供模板参数。
要解决这个问题,您需要将第43行的代码修改为以下形式:
```cpp
std::stack<your_data_type> Stack;
```
将 `your_data_type` 替换为您要在堆栈中存储的实际数据类型。
这样就可以正确地声明和使用带有模板参数的 `std::stack` 对象了。
in string requires string as left operant, not list
这个错误通常表示你在字符串操作中使用了列表,而不是字符串。你需要确保你的操作对象是字符串,而不是列表。
例如,如果你想把一个字符串中的某些字符替换成另一个字符,你需要把字符串作为左操作数,而不是列表。下面是一个例子:
```python
my_string = "hello world"
my_list = ["o", "l"]
new_string = my_string.replace(my_list[0], my_list[1])
print(new_string)
```
这个例子中,如果你把 my_list 作为左操作数,就会出现上述错误。因此,确保你正确使用字符串和列表,以避免这个错误。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)