2

Trying to compile my code with the following command: g++ Error.cpp -Wall -std=c++0x -o filename
I get a warning: Error.cpp:40:30: warning: unused variable ‘ostr’ [-Wunused-variable]

I've seen that the -Wall can be removed to suppress warnings but i don't want to do that. I want to put something in my code to work around it. Only been coding for ~6 months btw.

// 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;
}

EDIT: Yes line 40 is the line with the if. For some reason i had thought that const char*(ostr) would place m_message inside ostr and then it could be returned and outputted elsewhere. I didn't recognize that i was just creating a useless variable in the if statement, thought my operator overload would come into play though i'm not 100% sure if i'm using it correctly...

Chris
  • 40
  • 6
  • If you only want to suppress the warning inside the file you can take a look at this part of the documentation: https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html – UnholySheep Mar 14 '17 at 09:24
  • Which line is line 40? I bet it's the one with the `if`, where you're declaring a local variable `ostr` of type `const char*` (with unnecessary parentheses around its name). What did you want to achieve there? – Angew is no longer proud of SO Mar 14 '17 at 09:26
  • The warning is on the line `if (!bool(E)) { const char*(ostr); }`, which declares a variable named `ostr` that hides the `ostr` that is the function argument. Can you explain what you intend that statement to actually achieve ?? .... because, odds are, it is not achieving it. – Peter Mar 14 '17 at 09:39
  • FYI, we keep a list of [good C++ books](http://stackoverflow.com/q/388242/1782465) here on SO; you might want to take a look at one. – Angew is no longer proud of SO Mar 14 '17 at 10:24
  • Compiler errors and warnings tell you what line they originate on. Actually including this information in your actual question, and paying attention to it, is important when diagnosing C++ errors and warnings. – Yakk - Adam Nevraumont Mar 14 '17 at 12:52

2 Answers2

2

As this live example shows, the problem is not the function parameter ostr: that one is used by the return statement.

The problem is the local variable ostr of type const char * which you're declaring inside the if:

if (!bool(E)) { const char*(ostr); }

The parentheses are legal, but redundant: that line is equivalent to this:

if (!bool(E)) { const char *ostr; }

You're declaring a local variable (which happens to hide the function parameter), and not using it for anything.

If you want to stream the message from E into ostr, you'll have to do this:

if (!bool(E)) { ostr << static_cast<const char*>(E); }
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
0

Did you mean to do the following?

std::ostream& ict::operator<<(ostream& ostr, const Error& E) {
    if (!bool(E)) { ostr << E.m_message; }
    return ostr;
}
Frank-Rene Schäfer
  • 3,182
  • 27
  • 51