我收到这个错误:
error: cannot convert 'std::string {aka std::basic_string<char>}' to 'char*' for argument '1' to 'char* strcpy(char*, const char*)'
Run Code Online (Sandbox Code Playgroud)
我假设它意味着它无法将我的一个标题字符串分配给我的newtitle字符串,因为它们的类型不同.(我认为一个是char和另一个const char?)
strcpy(title, newtitle);
Run Code Online (Sandbox Code Playgroud)
但它们都被定义为类型字符串,所以我有点困惑的是它给了我这个错误是什么.虽然我错误地认为这个错误意味着什么.
#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);
}
Run Code Online (Sandbox Code Playgroud)
std::strcpy
期望它的第一个参数是char*
,但std::string
无法隐式转换为char*
,这就是编译器抱怨的原因.
你并不需要使用strcpy
的std::string
,你可以只
title = newtitle;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
172 次 |
最近记录: |