This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C++ compiler error: ambiguous call to overloaded function
just copied some code from a pdf into both C++builder XE2 and visual studio express 2012. both both compilers give error codes about ambiquity. I just started so i don't really know what to do. Maybe my textbook(pdf) is old and obsolete now? it's called "Learn c++ in 14 days". Well anyways here is the copied code.
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <stdio.h>
#pragma hdrstop
void getSqrRoot(char* buff, int x);
int main(int argc, char** argv)
{
int x;
char buff[30];
cout << “Enter a number: “;
cin >> x;
getSqrRoot(buff, x);
cout << buff;
getch();
}
void getSqrRoot(char* buff, int x)
{
sprintf(buff, “The sqaure root is: %f”, sqrt(x));
}
the errorcode i got in c++builder is:
[BCC32 Error] SquareRoot.cpp(19): E2015 Ambiguity between 'std::sqrt(float) at c:\program files (x86)\embarcadero\rad studio\9.0\include\windows\crtl\math.h:266' and 'std::sqrt(long double) at c:\program files (x86)\embarcadero\rad studio\9.0\include\windows\crtl\math.h:302'
Full parser context
SquareRoot.cpp(18): parsing: void getSqrRoot(char *,int)
On a side note, the quotation marks in my pdf manual are different characters than the normal " which i type. these “ are also not compatible with the compiler. maybe anybody knows a fix for this as well? thanks in advance.
Change your code like that:
void getSqrRoot(char* buff, int x)
{
sprintf(buff, “The sqaure root is: %f”, sqrt((float)x));
}
Because square root is overloaded function compiler has no opportunity to implicit conversion from int x value to float or double value, you need to do it directly.
Compiler: see sqrt(int) -> what to choose? sqrt(float)/sqrt(double) ?
Compiler: see sqrt((float)int) -> sqrt(float), ok!
Compeler: see sqrt((double)int) -> sqrt(double), ok!
Change your getSqrRoot Function to the below
void getSqrRoot(char* buff, float x)
{
And similarly fix the declaration in the first line.
This is happening because std::sqrt which is the function you are using to get the square root, can take either a float or a double but you have given it an int which leads to the confusion since the compiler now has no idea which function to call.
Related
Below is a snippet to show the basic structure of my code right now. On that last line, Visual Studio is saying "no instance of overloaded function that matches the argument list". According to the references I can find from VS help, however, this is exactly how the function should be used. I have string and iostream included, and I have precompiled headers turned off, so I'm not sure if the fault for VS not recognizing this function is on me or not.
#include <iostream>
#include <vector>
#include <string>
int main(){
Stack<double> nums;
std::string input;
std::string::size_type index;
std::cin >> input;
double num = std::stod(input, index);
}
It should be a pointer to size_type. Verified in VS.
double num = std::stod(input, &index);
When overloading is around, you must be precise :)
Look my code seems to be correct, according to all the documentation I can find online. My IDE is MS Visual Studio Xpress 4 Windows Desktop 2012, and it's compiler is throwing up the error:
Error 1 error C3861: 'setenv': identifier not found e:\users\owner\documents\visual studio 2012\projects\project1\project1\source1.cpp 18 1 Project1.
Help me!!!
#include <windows.h>
#include <sstream>
#include <ostream>
#include <cstdlib>
#include <iostream>
#include <stdlib.h>
using namespace std;
int howManyInClass = 0;
int main(){
long checklength = sizeof(getenv("classSize"))/sizeof(*getenv("classSize"));
if (checklength==0){
cout<<"Please enter the ammount of students in your class";
cin>> howManyInClass;
cin.ignore();
setenv("classSize", howManyInClass, 1);}
};
Microsoft's runtime library doesn't support the standard setenv() function. You could use their replacement _putenv() or, for portable code, I prefer to use a simple wrapper.
Here's my wrapper with the standard interface:
int setenv(const char *name, const char *value, int overwrite)
{
int errcode = 0;
if(!overwrite) {
size_t envsize = 0;
errcode = getenv_s(&envsize, NULL, 0, name);
if(errcode || envsize) return errcode;
}
return _putenv_s(name, value);
}
You can either use _putenv() which takes a string parameter as the string classSize=7;
ostringstream classSize;
classSize << "classSize=" << howManyInClass;
_putenv(classSize.str().c_str());
...or (preferably) the security enhanced _putenv_s() that takes the key and the value as separate (const char*) parameters;
ostringstream classSize;
classSize << howManyInClass;
_putenv_s("classSize", classSize.str().c_str());
Try _putenv instead of setenv.
msdn _putenv
the reason you encountered the linkage error is that, if you take a look at the content of the library of stdlib.h, you will find that, setenv() is not declared there. At the first glance, it is a C standard API, but looks like Windows do not follow all of the standard. Or, you might be able to configure your VS to use CRT instead of Windows runtime, in that case, I think setenv will be identified.
Visual Studio is going crazy on me recently, and gives me the error in the subject when all I did was a simple cout...
CODE:
// Lang.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main{
cout << "hi";
}
int main{
cout << "hi";
}
Due to the possibility of initialising objects in C++ with the {} syntax, your compiler probably interprets this code as an attempt to create a global int variable called main, initialised with the result of std::ostream::operator<<, a member function which returns a reference to the std::ostream itself.
It's as if you had written:
double some_variable { cout << "hi" }
Or:
double some_variable { cout }
std::ostream is actually std::basic_ostream<char, std::char_traits<char>>. And that type is not compatible with int.
The only thing which is strange is why the ; after "hi" does not immediately cause the compiler to stop trying; but you don't say which compiler version and which options you are using.
In any case, all of those facts finally result in the error message:
no suitable conversion function from “std::basic_ostream<char,
std::char_traits<char>>” to “int” exists
and in:
Also, the semicolon after "hi" is highlighted, and says "expected a }"
Solution: make main a function:
int main() {
cout << "hi";
}
I was overloading type casting operators, and internal error has occurred in Visual Studio 2013.
This is the header of the exponent class:
#pragma once
#include "Calc.h"
#include <iostream>
using namespace std;
class Exponent
{
private:
int base;
int exponent;
public:
Exponent();
Exponent(int a)
{
base = a;
}
int getBase()
{
return base;
}
};
void printExp(Exponent e)
{
cout << e.getBase() << endl;
}
and this is calc.h I wrote that will contain overloaded type casting function:
#pragma once
#include "Exponent.h"
class Calc
{
private:
int acc;
public:
Calc();
Calc(int a);
operator Exponent() //this is where I get an error.
{
return Exponent(acc);
}
};
And here's the main function:
#include "stdafx.h"
#include <iostream>
#include "Exponent.h"
#include "Calc.h"
using namespace std;
int main()
{
Calc c(6);
printExp(c);
return 0;
}
I have no idea why I get an error here:
operator Exponent() //this is where I get an error.
{
return Exponent(acc);
}
This is somehow making Visual Studio crash, showing an error like this:
Microsoft(R) C\C++ Optimizing compiler has stopped working...
An internal compiler error (ICE) is a compiler bug—a well-functioning compiler should always report sensible errors for erroneous code. I'd suggest you file a bug report on connect.microsoft.com.
However, you do have a clear problem with your code: you have a circular dependency between your two header files. Exponent.h includes Calc.h, which in turn includes Exponent.h. Even if the compiler weren't crashing with an ICE, it would still report an error (likely of the "undefined symbol" variety) with this code.
There's a simple fix to this case—because Exponent.h doesn't actually have any dependencies on Calc.h, you can just remove the #include "Calc.h" line from Exponent.h, and the circular dependency will be gone.
This code no longer crashes in Visual Studio "14" CTP.
It won't compile because you included Calc.h in Exponent.h, which means the compiler sees class Calc before it sees class Exponent, so you're trying to define a conversion operator to a type that the compiler doesn't know exists yet.
Look my code seems to be correct, according to all the documentation I can find online. My IDE is MS Visual Studio Xpress 4 Windows Desktop 2012, and it's compiler is throwing up the error:
Error 1 error C3861: 'setenv': identifier not found e:\users\owner\documents\visual studio 2012\projects\project1\project1\source1.cpp 18 1 Project1.
Help me!!!
#include <windows.h>
#include <sstream>
#include <ostream>
#include <cstdlib>
#include <iostream>
#include <stdlib.h>
using namespace std;
int howManyInClass = 0;
int main(){
long checklength = sizeof(getenv("classSize"))/sizeof(*getenv("classSize"));
if (checklength==0){
cout<<"Please enter the ammount of students in your class";
cin>> howManyInClass;
cin.ignore();
setenv("classSize", howManyInClass, 1);}
};
Microsoft's runtime library doesn't support the standard setenv() function. You could use their replacement _putenv() or, for portable code, I prefer to use a simple wrapper.
Here's my wrapper with the standard interface:
int setenv(const char *name, const char *value, int overwrite)
{
int errcode = 0;
if(!overwrite) {
size_t envsize = 0;
errcode = getenv_s(&envsize, NULL, 0, name);
if(errcode || envsize) return errcode;
}
return _putenv_s(name, value);
}
You can either use _putenv() which takes a string parameter as the string classSize=7;
ostringstream classSize;
classSize << "classSize=" << howManyInClass;
_putenv(classSize.str().c_str());
...or (preferably) the security enhanced _putenv_s() that takes the key and the value as separate (const char*) parameters;
ostringstream classSize;
classSize << howManyInClass;
_putenv_s("classSize", classSize.str().c_str());
Try _putenv instead of setenv.
msdn _putenv
the reason you encountered the linkage error is that, if you take a look at the content of the library of stdlib.h, you will find that, setenv() is not declared there. At the first glance, it is a C standard API, but looks like Windows do not follow all of the standard. Or, you might be able to configure your VS to use CRT instead of Windows runtime, in that case, I think setenv will be identified.