如果我分配给彼此的两个变量都是字符串类型,为什么我会收到这个strcpy赋值错误
Why am I receiving this strcpy assignment error if the two variables I am assigning to each other are both of type string?
我收到这个错误:
error: cannot convert 'std::string {aka std::basic_string<char>}' to 'char*' for argument '1' to 'char* strcpy(char*, const char*)'
我认为这意味着它无法将我的一个标题字符串分配给我的新标题字符串,因为它们不属于同一类型。(我想一个是char,另一个是const char?)
strcpy(title, newtitle);
但它们都被定义为类型字符串,所以我有点困惑是什么导致了这个错误。尽管我对这个错误的确切含义可能是错的。
#include<iostream>
#include<iomanip>
using namespace std;
#include <cstring>
class Movie{
private:
string title;
int year;
string director;
public:
void setTitle(string); // function prototype
void setYear(int); // function prototype
void setDirector(string); // function prototype
void displayMovie(); // function prototype
};
void Movie::setTitle(string newtitle)
{
strcpy(title, newtitle);
}
int main()
{
Movie myMovie;
string movietitle;
cout << "Enter the title of the Movie: " << endl;
cin >> movietitle;
myMovie.setTitle(movietitle);
}
std::strcpy
期望其第一个参数为char*
,但std::string
无法隐式转换为char*
,这就是编译器抱怨的原因。
std::string
不需要使用strcpy
,只需即可
title = newtitle;
相关文章:
- 使用简单类型列表实现的指数编译时间.为什么
- 为什么与常规GCC不同,即使有"学究性错误",MinGW-GCC也能容忍丢失的返回类型
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 为什么类型 char 返回不同的值?
- 为什么类型铸件仅在这句话中失败
- 为什么类型变量;不调用默认 CTR
- 为什么类型转换对象不会更改其地址?有关对象类型的信息存储在哪里?
- 为什么类型关键字以"_t"后缀结尾?
- 为什么类型特征不适用于命名空间范围内的类型?
- 为什么类型可转换性C++不传递
- 为什么 T& 类型的模板函数参数可以绑定到常量左值而不是右值?
- 为什么类型为 sf::Text 的对象返回不同的 getPosition().y 和 getLocalBounds().
- 为什么类型扣除会失败(非销售)功能类型
- 为什么C类型泛型表达式不能与C++兼容
- 为什么类型是char
- 为什么C++类型的表达式没有从左到右进行解释
- 为什么类型长赋值中的文本值在 C++ 的末尾仍然有一个'L'?
- 为什么类型转换运算符不适用于继承的类?
- 为什么类型推导不能按预期工作
- 为什么类型知识在Boost::MPL中消失了?