为什么g++在未执行的代码处标记强制转换错误
Why does g++ mark a casting error at not executed code?
main:
#include "multiparse.h"
int main()
{
parse_string obj;
obj = "1234";
//int a = obj;
//obj = 1234;
return 0;
}
标题:
class parse_string
{
char* str;
long str_sz;
double val;
bool isnumber;
public:
template<class typename>
parse_string& operator=(typenamet input)
{
//printf("%d == %dn",typeid(input).name(),typeid(const char*).name());
if(typeid(input)==typeid(char*)||typeid(input)==typeid(const char*))
{
str_sz=strlen(input)+1;
if(str==0)
{
str = (char*)malloc(sizeof(char)*str_sz);
}
else
{
str = (char*)realloc(str,sizeof(char)*str_sz);
}
memset(str,0,str_sz);
strcpy(str,input);
this->str_to_num();
isnumber=0;
printf("An");
}
else
{
printf("Bn");
val = (double)input;
this->num_to_str();
isnumber=1;
}
}
};
g++错误:multiparse.h错误:-->"val=(double(input;"处从类型"const char*"到类型"double"的强制转换无效
在我的情况下,这段代码不会被执行——它只会打印"A"而不是"B",但g++不会编译这段代码。我想不通。
即使代码没有执行,它仍然是*.cpp
文件的一部分(就像以前的#include
d一样(。因此,它成为该源的*.obj
/*.o
文件的一个部分。要做到这一点,编译器需要为*.cpp
文件中的所有内容生成machine code
(模板的工作方式有点不同,但现在与它们无关(。
换句话说,要获得一个由.obj
文件组成的.exe
/.lib
/.dll
文件,需要将要成为所述.obj
文件的文件正确编译(转码为machine code
(。
我为我的需求找到了一个解决方案,但我认为这不是最好的方法:
parse_string& operator=(char* input)
{
str_sz=strlen(input)+1;
if(str==0)
{
str = (char*)malloc(sizeof(char)*str_sz);
}
else
{
str = (char*)realloc(str,sizeof(char)*str_sz);
}
memset(str,0,str_sz);
strcpy(str,input);
this->str_to_num();
isnumber=0;
return *this;
}
parse_string& operator=(const char* input)
{
str_sz=strlen(input)+1;
if(str==0)
{
str = (char*)malloc(sizeof(char)*str_sz);
}
else
{
str = (char*)realloc(str,sizeof(char)*str_sz);
}
memset(str,0,str_sz);
strcpy(str,input);
this->str_to_num();
isnumber=0;
return *this;
}
parse_string& operator=(char input)
{
val = (double)input;
this->num_to_str();
isnumber=1;
return *this;
}
parse_string& operator=(int input)
{
val = (double)input;
this->num_to_str();
isnumber=1;
return *this;
}
parse_string& operator=(long input)
{
val = (double)input;
this->num_to_str();
isnumber=1;
return *this;
}
parse_string& operator=(unsigned char input)
{
val = (double)input;
this->num_to_str();
isnumber=1;
return *this;
}
parse_string& operator=(unsigned int input)
{
val = (double)input;
this->num_to_str();
isnumber=1;
return *this;
}
parse_string& operator=(unsigned long input)
{
val = (double)input;
this->num_to_str();
isnumber=1;
return *this;
}
parse_string& operator=(double input)
{
val = input;
this->num_to_str();
isnumber=1;
return *this;
}
相关文章:
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- C++使用params创建线程函数会导致转换错误
- 在混合代码库中将C转换为C++时出现许多包含错误
- 为什么g++在未执行的代码处标记强制转换错误
- 错误:从"int"到枚举c++的转换无效
- 错误 C2679:二进制"<<":未找到采用类型 'std::string_view' 的右侧操作数的运算符(或者没有可接受的转换)
- 从标准::未来<void>到非标量标准::未来<bool>引发的错误转换
- 使用宏的错误转换
- 在 opencv 中使用 Color_YUV2BGR 从 YUV 到 BGR 的错误转换
- 无法通过此操作,因为类型的错误转换
- PyQt5:使用自定义信号时 int 对象的错误转换
- 从长途错误转换
- C 错误:转换为非量表类型
- C 错误:转换为执行字符集
- 使用SOCI从PostgreSQL数据库获取数据时发生错误转换
- 分段错误C++转换问题
- 在ARM上的Qt中从双精度到QString的错误转换
- C++浮点转换为Python浮点错误转换
- 错误:转换为非标量类型