{"id":492,"date":"2022-08-30T15:02:15","date_gmt":"2022-08-30T15:02:15","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/about-assignment-operator-over-loading-error-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:02:15","modified_gmt":"2022-08-30T15:02:15","slug":"about-assignment-operator-over-loading-error-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/about-assignment-operator-over-loading-error-collection-of-common-programming-errors\/","title":{"rendered":"About assignment operator over loading error-Collection of common programming errors"},"content":{"rendered":"<p>Please explain the error message for this program ..<\/p>\n<pre><code>#include \nusing namespace std;\nclass copyConst\n{\n\n    private:\n    int someVal;\n    public:\n    copyConst(const copyConst &amp;objParam)\n    {\n        someVal = objParam.someVal;\n    }\n    copyConst()\n    {\n        someVal = 9;\n    }\n    copyConst&amp; operator=(const copyConst &amp;objParam)\n    {\n        if (this == &amp;objParam)\n            return *this;\n\n        someVal = objParam.someVal;\n        return *this;\n        }\n    };\n\nint main(int argc, char **argv)\n{\n    copyConst obj1;\n    copyConst obj2(obj1);\n    copyConst obj3 = obj1;\n    copyConst obj4;\n    obj4 = obj1;\n\n\n    return 0;\n}\n<\/code><\/pre>\n<p>ERROR MESSAGE:<\/p>\n<blockquote>\n<p>gcc -Wall -o &#8220;untitled&#8221; &#8220;untitled.cpp&#8221; (in directory: \/home\/rwik\/Documents) untitled.cpp: In function \u2018int main(int, char**)\u2019: untitled.cpp:53:12: warning: variable \u2018obj3\u2019 set but not used [-Wunused-but-set-variable] \/tmp\/ccUIyRPg.o: In function <code>__static_initialization_and_destruction_0(int, int)': untitled.cpp:(.text+0x8a): undefined reference to<\/code> std::ios_base::Init::Init()&#8217; untitled.cpp:(.text+0x8f): undefined reference to `std::ios_base::Init::~Init()&#8217; Compilation failed. collect2: ld returned 1 exit status<\/p>\n<\/blockquote>\n<ol>\n<li>\n<p>There are two type of warning messages. The second one is because of missing linking flag in gcc: <code>gcc -lstdc++ -Wall -o \"untitled\" \"untitled.cpp\"<\/code> (or the equivalent <code>g++ -Wall -o \"untitled\" \"untitled.cpp<\/code>.<\/p>\n<p>The first warning regarding unused variable is because of <code>obj3<\/code> variable which is declared but not used anywhere else. For such cases I&#8217;m using <code>(void)obj3;<\/code> statement to workaround such warning messages.<\/p>\n<\/li>\n<li>\n<p>Compile using <code>g++<\/code>, not <code>gcc<\/code>. You have C++ code, not C code.<\/p>\n<p>It has nothing to do with the class code.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 19:46:06. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Please explain the error message for this program .. #include using namespace std; class copyConst { private: int someVal; public: copyConst(const copyConst &amp;objParam) { someVal = objParam.someVal; } copyConst() { someVal = 9; } copyConst&amp; operator=(const copyConst &amp;objParam) { if (this == &amp;objParam) return *this; someVal = objParam.someVal; return *this; } }; int main(int argc, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-492","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/492","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=492"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/492\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}