抑制警告:未使用的变量
Suppress warning: unused variable
尝试使用以下命令编译我的代码:
g++ Error.cpp -Wall -std=c++0x -o 文件名
我收到警告:Error.cpp:40:30: 警告:未使用的变量‘osr’[-Wunused-variable]
我看到可以移除 -Wall 以抑制警告,但我不想那样做。我想在我的代码中添加一些东西来解决它。顺便说一句,只编码了 ~6 个月。
// Error.cpp
#define _CRT_SECURE_NO_WARNINGS
// Tried using #define _CRT_UNUSED then doing _CRT_UNUSED(ostr) down below
#include <iomanip>
#include <iostream>
#include <cstring>
#include "Error.h"
void Error::message(const char* errorMessage) {
clear();
m_message = new char[strlen(errorMessage) + 1];
strcpy(m_message, errorMessage);
}
void Error::operator=(const char* errorMessage) {
message(errorMessage);
}
Error::operator const char*() const {
return m_message;
}
Error::operator bool() const {
return m_message == nullptr;
}
std::ostream& ict::operator<<(ostream& ostr, const Error& E) {
(void)ostr; // This didn't work, i still get the warning with or without it
if (!bool(E)) { const char*(ostr); }
return ostr;
}
编辑:是的,第 40 行是带有 if 的行。出于某种原因,我曾认为 const char*(ostr)
会将 m_message
放在 ostr
中,然后它可以返回并输出到其他地方。我没有意识到我只是在 if 语句中创建了一个无用的变量,以为我的运算符重载会起作用,尽管我不能 100% 确定我是否正确使用它...
如 this live example 所示,问题不在于函数参数 ostr
:return 语句使用了那个参数。
问题是您在 if
:
中声明的 const char *
类型的局部变量 ostr
if (!bool(E)) { const char*(ostr); }
括号是合法的,但多余:该行等同于:
if (!bool(E)) { const char *ostr; }
您正在声明一个局部变量(恰好隐藏了函数参数),并且没有将其用于任何用途。
如果您想将消息从 E
流式传输到 ostr
,您必须这样做:
if (!bool(E)) { ostr << static_cast<const char*>(E); }
您是要执行以下操作吗?
std::ostream& ict::operator<<(ostream& ostr, const Error& E) {
if (!bool(E)) { ostr << E.m_message; }
return ostr;
}
尝试使用以下命令编译我的代码:
g++ Error.cpp -Wall -std=c++0x -o 文件名
我收到警告:Error.cpp:40:30: 警告:未使用的变量‘osr’[-Wunused-variable]
我看到可以移除 -Wall 以抑制警告,但我不想那样做。我想在我的代码中添加一些东西来解决它。顺便说一句,只编码了 ~6 个月。
// Error.cpp
#define _CRT_SECURE_NO_WARNINGS
// Tried using #define _CRT_UNUSED then doing _CRT_UNUSED(ostr) down below
#include <iomanip>
#include <iostream>
#include <cstring>
#include "Error.h"
void Error::message(const char* errorMessage) {
clear();
m_message = new char[strlen(errorMessage) + 1];
strcpy(m_message, errorMessage);
}
void Error::operator=(const char* errorMessage) {
message(errorMessage);
}
Error::operator const char*() const {
return m_message;
}
Error::operator bool() const {
return m_message == nullptr;
}
std::ostream& ict::operator<<(ostream& ostr, const Error& E) {
(void)ostr; // This didn't work, i still get the warning with or without it
if (!bool(E)) { const char*(ostr); }
return ostr;
}
编辑:是的,第 40 行是带有 if 的行。出于某种原因,我曾认为 const char*(ostr)
会将 m_message
放在 ostr
中,然后它可以返回并输出到其他地方。我没有意识到我只是在 if 语句中创建了一个无用的变量,以为我的运算符重载会起作用,尽管我不能 100% 确定我是否正确使用它...
如 this live example 所示,问题不在于函数参数 ostr
:return 语句使用了那个参数。
问题是您在 if
:
const char *
类型的局部变量 ostr
if (!bool(E)) { const char*(ostr); }
括号是合法的,但多余:该行等同于:
if (!bool(E)) { const char *ostr; }
您正在声明一个局部变量(恰好隐藏了函数参数),并且没有将其用于任何用途。
如果您想将消息从 E
流式传输到 ostr
,您必须这样做:
if (!bool(E)) { ostr << static_cast<const char*>(E); }
您是要执行以下操作吗?
std::ostream& ict::operator<<(ostream& ostr, const Error& E) {
if (!bool(E)) { ostr << E.m_message; }
return ostr;
}