当表达式是赋值语句时,且左值表达式类型已知,但右值表达式中有未定义类型,此时g++不会报‘类型未定义’,而报‘invalid conversion’
先来简单的场景,左值表达式和右值表达式都未定义
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
A a = new A;
return 0;
}
报错
E:\Projects\Codeblocks\StudyCpp\main.cpp|7|error: 'A' was not declared in this scope|
E:\Projects\Codeblocks\StudyCpp\main.cpp|7|error: expected ';' before 'a'|
再来体现问题的场景,左值已知,右值未知
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int a = new A;
return 0;
}
报错
E:\Projects\Codeblocks\StudyCpp\main.cpp|7|error: expected type-specifier before 'A'|
E:\Projects\Codeblocks\StudyCpp\main.cpp|7|error: invalid conversion from 'int*' to 'int' [-fpermissive]|
E:\Projects\Codeblocks\StudyCpp\main.cpp|7|error: expected ',' or ';' before 'A'|
所以,当你遇到invalid conversion时,检查一下右值表达式的类是否定义,或类所在头文件是否正确包含。