About assignment operator over loading error-Collection of common programming errors
Please explain the error message for this program ..
#include
using namespace std;
class copyConst
{
private:
int someVal;
public:
copyConst(const copyConst &objParam)
{
someVal = objParam.someVal;
}
copyConst()
{
someVal = 9;
}
copyConst& operator=(const copyConst &objParam)
{
if (this == &objParam)
return *this;
someVal = objParam.someVal;
return *this;
}
};
int main(int argc, char **argv)
{
copyConst obj1;
copyConst obj2(obj1);
copyConst obj3 = obj1;
copyConst obj4;
obj4 = obj1;
return 0;
}
ERROR MESSAGE:
gcc -Wall -o “untitled” “untitled.cpp” (in directory: /home/rwik/Documents) untitled.cpp: In function ‘int main(int, char**)’: untitled.cpp:53:12: warning: variable ‘obj3’ set but not used [-Wunused-but-set-variable] /tmp/ccUIyRPg.o: In function
__static_initialization_and_destruction_0(int, int)': untitled.cpp:(.text+0x8a): undefined reference to
std::ios_base::Init::Init()’ untitled.cpp:(.text+0x8f): undefined reference to `std::ios_base::Init::~Init()’ Compilation failed. collect2: ld returned 1 exit status
-
There are two type of warning messages. The second one is because of missing linking flag in gcc:
gcc -lstdc++ -Wall -o "untitled" "untitled.cpp"
(or the equivalentg++ -Wall -o "untitled" "untitled.cpp
.The first warning regarding unused variable is because of
obj3
variable which is declared but not used anywhere else. For such cases I’m using(void)obj3;
statement to workaround such warning messages. -
Compile using
g++
, notgcc
. You have C++ code, not C code.It has nothing to do with the class code.
Originally posted 2013-11-09 19:46:06.