C++;错误:从‘;字符*’;至‘;int’;失准 我几天前开始学习C++。

C++;错误:从‘;字符*’;至‘;int’;失准 我几天前开始学习C++。,c++,C++,我想使用用户输入设置dog.age #include <iostream> using namespace std; class dog{ public: dog(); ~dog(); int getAge(); void setAge(int a); protected: int age; }; dog::dog(){ } dog::~dog(){ } int dog::

我想使用用户输入设置
dog.age

#include <iostream>

using namespace std;

class dog{
    public:
        dog();
        ~dog();
        int getAge();
        void setAge(int a);

    protected:
        int age;
};

dog::dog(){

}

dog::~dog(){

}

int dog::getAge(){
    return age;
}

void dog::setAge(int a){
    age = a;
}

int main(){
    dog myDog;
    char myString[2];
    int age;

    cout<<"How old is the dog? ";
    cin.getline(myString,2,'\n');

    age = (int)myString;
    myDog.setAge(age);
    cout<<"The dog is "<<myDog.getAge()<<" years old!\n";
    return 0;
}
即使我删除
(int)
,它也会失败

为什么我的程序不将
myString
转换为
int


可选项:如果我在构建类时犯了其他错误,请随时告诉我。我想早点改掉坏习惯。

这样就不能将字符串转换为int
myString
属于
char[]
类型,该类型的强制转换将衰减为
char*
,然后转换为
int

标准库包含一些可以从字符串转换为
int
的方法


示例:

不能使用简单的强制转换将字符串转换为数字。@πάνταῥεῖ 我还必须采取什么其他步骤?
error: cast from ‘char*’ to ‘int’ loses precision [-fpermissive]
  age = (int)myString;`