c++ - error C2440: '=': cannot convert from 'div_t' to 'double' -


i'm beginner , english not sorry first. 1. im trying compile code , dont understand problem- function div returns 'double', why can't write line: "sum= div(x, y);" ? tried find answer here , google doesnt work. 2. beyond that, knows solution problem- 1>done building project "test.vcxproj" -- failed. ? thank answers!

#pragma once #include <iostream> #include <string.h>  using namespace std; class divisionbyzero { private:     const char* zero_description = "error: division zero"; public:     const char* getzero() {return zero_description;}     void printzero() { cout << getzero() << endl; } };  double div(double x, double y) throw(int, divisionbyzero) {     if (x < y)         throw - 1;     if (y == 0)         throw divisionbyzero();     cout << x / y << endl;     return x / y; }   #include "divisionbyzero.h"  using namespace std;  int main() {     try {         int x, y;         double sum;         cout << "insert x ,  y values" << endl;         cin >> x >> y;         sum= div(x, y);     }     catch (int a) {         if (a == -1)             cout << "x smaller y" << endl;     }     catch (divisionbyzero& c) {         c.printzero();     }     catch (...) {         cout << "unknown error" << endl;     }     return 0; } 

the problem standard library contains a div function, takes 2 ints , returns div_t structure. because you're passing 2 ints in div(x, y) function call, overload resolution picks std::div on own div function. end trying assign div_t structure double variable, results in error message you're seeing.

some compilers bring div top-level namespace (i'm not sure if defined standard), without using namespace std; might not work.

the easiest fix (and least confusing other humans reading code) rename own div function else.

this 1 of reasons using namespace std; frowned upon some. it's bad practice put using namespace directives in header file, because pollute scope of source file including header. (i'm assuming first part of code snippet contents of divisionbyzero.h.)


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -