c++ - 有一个参数字符串问题的构造函数
问题描述
首先,我不是一个经验丰富的 C++ 程序员,我只是想通过我的面向对象编程考试。我知道如果我有一个只有一个参数的类构造函数,如果我做这样的事情 -> myClass object = something
就像传递something
给带有一个参数的构造函数,对吧?那么,为什么这可以正常工作:
#include <iostream>
using namespace std;
class A
{
public:
int m;
A(int M){
m = M;
}
print(){
cout << "m: " << m << "\n";
}
};
int main()
{
A x = 132;
x.print();
return 0;
}
这不会:
#include <iostream>
using namespace std;
class A
{
public:
string m;
A(string M){
m = M;
}
print(){
cout << "m: " << m << "\n";
}
};
int main()
{
A x = "RANDOMSTRING";
x.print();
return 0;
}
我在第二种情况下得到的错误是:
error: conversion from 'const char [13]' to non-scalar type 'A' requested
我想这是该字符串参数的某种问题,但是为什么呢?
解决方案
推荐阅读
- java - 如何消除paint() 方法中的闪烁
- java - 如何并行调度多个 Flux 并限制 onNext 和 onRequest 事件
- flutter - 飞镖中有没有办法标记方法,调试器不会介入?
- css - 我没有成功垂直对齐标签内的文本
- azure-devops - 如何将自定义 dll 从私有存储库添加到 azure dev ops 管道?
- java - 使用具有参数化的 jenkins 执行 Aws 设备场
- rust - 从外部调用的函数访问结构
- reactjs - Material UI - 选项卡:如何分组或折叠选项卡
- python - 通过 Python 在 DigitalMicrograph 中注释
- javascript - 在 Javascript 中,如果我有一个很长的标记模板文字,如何包装它而不在结果字符串中放置换行符?