g++: error: ambiguous overload for ‘operator>>’ in ‘std::cin >> ((((int)
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
g++: error: ambiguous overload for ‘operator>>’ in ‘std::cin >> ((((int)
Any idea what that error in the title means?
Here is my code:
Code:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
char null_char = 0;
string image_file;
int bs;
int count;
cout << "zerowrite version 0.1." << endl << endl << "This program is free software; you can copy, distribute, and modify it as you please under the terms of the GNU General Public License version 3 (GPLv3). Adding proprietary code to this project prohibited." << endl << endl << "Image file name to write: ";
cin >> image_file;
cout << "Block size of individual blocks within file: ";
cin >> bs;
cout << "Number of blocks: ";
cin >> count;
cout << "Writing image, please wait... ";
cin >> null_char*bs*count >> image_file;
return 0;
}
Is the problem that I cannot cin the same value twice or something? If so, how do I get around the problem?
I can't guess what that line of code was supposed to accomplish. The compiler doesn't try to guess. The specified operation is invalid and generates an error. Unfortunately, templates are complicated enough that the compiler can't select a very appropriate error message. "Ambiguous overload" doesn't describe this error very well. There is an invalid right hand side operand for operator>>(). "Ambiguous" ought to imply there is some version of the function that would be valid if other version(s) weren't equally good fits.
Actually, I can guess what the line is supposed to accomplish: Write bs*count characters, each of value null_char to the file image_file. But as I said before, the compiler won't make guesses, especially not guesses that wildly different from what the code actually says.
cin is input, cout is output. This is what I have read in plenty of C++ books; it's a shame no one here knows what it is.
From my personal book on C++:
Quote:
You can obtain input from the keyboard through the stream cin, using the extraction operator for a stream >>. To read two integer values from the keyboard into integer values num1 and num2, you can write this statement:
Code:
cin >> num1 >> num2
The extraction operator, >>, "points" in the direction the data flows -- in this case, from cin to each of the variables in turn. Any leading whitespace is skipped, and the first integer value you key in is read into num1. This is because the input statement executes from left to right. Any whitespace following num1 is ignored and the second integer value that you enter is read into num2. There has to be some whitespace between successive values so that they can be differentiated. The stream input operation ends when you press the Enter key, and execution then continues with the next statement.
cin is input, cout is output. This is what I have read in plenty of C++ books; it's a shame no one here knows what it is.
From my personal book on C++:
Makes sense now?
No, your code makes even less sense now. In order to get something from a stream and store it somewhere you need an address of that somewhere to store in. A variable does have an address, an expression does not have an address.
It's a shame you do not understand such simple things.
In addition to the above post - Kenny, with your level of understanding of basic things you better consider C++ as a syntactic sugar to "C". I.e. if you want to do something in C++, first make sure you know you can do the same things in "C", and then when it works in "C", write it in C++.
I.e. first implement your input using 'fscanf' and friends, and then use the C++ syntax/template sugar.
cin is input, cout is output. This is what I have read in plenty of C++ books; it's a shame no one here knows what it is.
Most, maybe all, the people answering this thread know what cin and cout are and know what is wrong in your code.
It's a shame you prefer to insult the people helping you rather than read the answers you already have to your question.
Three of us have commented on the specific incorrect line in your code. If you need further help, you should explain what you wanted that specific line of code to do.
Compiles fine now that I used C instead of C++, but when I try to run the program I get a segmentation fault:
Code:
kenny-strawn@kennystrawn-laptop:~$ ./zerowrite
zerowrite version 0.1.
This program is free software; you can copy, distribute, and modify it as you please under the terms of the GNU General Public License version 3 (GPLv3). Adding proprietary code to this project prohibited.
Enter block size of individual blocks within raw image file: 1024
Enter number of blocks: 1024
Segmentation fault (core dumped)
kenny-strawn@kennystrawn-laptop:~$ ./zerowrite
zerowrite version 0.1.
This program is free software; you can copy, distribute, and modify it as you please under the terms of the GNU General Public License version 3 (GPLv3). Adding proprietary code to this project prohibited.
Enter block size of individual blocks within raw image file: 128
Enter number of blocks: 128
Segmentation fault (core dumped)
kenny-strawn@kennystrawn-laptop:~$
There were two warnings in the compilation process:
Code:
gcc -Wall -o "zerowrite" "zerowrite.c" (in directory: /home/kenny-strawn)
zerowrite.c: In function ‘main’:
zerowrite.c:17: warning: passing argument 1 of ‘fwrite’ makes pointer from integer without a cast
//usr/include/stdio.h:708: note: expected ‘const void * __restrict__’ but argument is of type ‘char’
zerowrite.c:17: warning: ‘image_file’ is used uninitialized in this function
Compilation finished successfully.
However, I wouldn't expect these warnings to cause a segfault.
Actually, I can guess what the line is supposed to accomplish: Write bs*count characters, each of value null_char to the file image_file. But as I said before, the compiler won't make guesses, especially not guesses that wildly different from what the code actually says.
Ah, my guess was less radical: just output null_char*bs*count and image_file (ie I meant change >> to << as well as cin to cout).
Quote:
Originally Posted by Kenny_Strawn
However, I wouldn't expect these warnings to cause a segfault.
I'm just guessing from the warnings since you posted no code, that fwrite() is called incorrectly. Using an integer as a pointer can certainly cause a segfault.
There were two warnings in the compilation process:
Code:
gcc -Wall -o "zerowrite" "zerowrite.c" (in directory: /home/kenny-strawn)
zerowrite.c: In function ‘main’:
zerowrite.c:17: warning: passing argument 1 of ‘fwrite’ makes pointer from integer without a cast
//usr/include/stdio.h:708: note: expected ‘const void * __restrict__’ but argument is of type ‘char’
zerowrite.c:17: warning: ‘image_file’ is used uninitialized in this function
Compilation finished successfully.
However, I wouldn't expect these warnings to cause a segfault.
Well the warning highlighted would indicate to me that what you want to write out to the output stream is not a valid pointer and so when the function treats it as such you will more than likely get a segmentation fault.
Going back to your original code (switching languages because something doesn't quite work is unlikely to be a satisfying solution)
Quote:
Originally Posted by Kenny_Strawn
Any idea what that error in the title means?
Here is my code:
Code:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
char null_char = 0;
string image_file;
int bs;
int count;
cout << "zerowrite version 0.1." << endl << endl << "This program is free software; you can copy, distribute, and modify it as you please under the terms of the GNU General Public License version 3 (GPLv3). Adding proprietary code to this project prohibited." << endl << endl << "Image file name to write: ";
cin >> image_file;
cout << "Block size of individual blocks within file: ";
cin >> bs;
cout << "Number of blocks: ";
cin >> count;
cout << "Writing image, please wait... ";
cin >> null_char*bs*count >> image_file;
return 0;
}
Is the problem that I cannot cin the same value twice or something? If so, how do I get around the problem?
What is it that you are trying to do. Specifically (as others have asked) with the highlighted line.
Remember you cannot assign into an expression, and so
Code:
cin>>null_char*bs*count
is meaningless.
If you are trying to read from the file given by image_file then you don't use cin (that is reading from the console not from a file). You will need to create your own input stream using the image_file
Code:
ifstream infile(image_file);
if(! infile)
{
// error processing here
}
// now you can start reading from the file
If you want to use a type other than char as the template then look at basic_ifstream or maybe basic_istream
I am trying to print an array of null characters to a raw image file that can then be formatted -- and so I know that fwrite is the way to do it. I also had to translate the C++ into C so that I could use fwrite.
Here is how I am trying to use fwrite:
Code:
#include <stdio.h>
#include <math.h>
int main() {
FILE *image_file;
int bs;
int count;
printf("zerowrite version 0.1.\n\n This program is free software; you can copy, distribute, and modify it as you please under the terms of the GNU General Public License version 3 (GPLv3). Adding proprietary code to this project prohibited.\n\n Enter block size of individual blocks within raw image file: ");
scanf("%d", &bs);
printf("Enter number of blocks: ");
scanf("%d", &count);
printf("Writing raw image, please wait... ");
char *null_char = 0;
fwrite(null_char, bs, count, image_file);
return 0;
}
Essentially, it is supposed to be an interactive alternative to dd for writing image files TBF (To Be Formatted).
And here's why I am not using /dev/zero: I want this program to be portable, as such that it could be ported to Windows with a simple invocation of MinGW. And I also want it to be more user friendly.
fwrite() is available in C++ check the <cstdio> headers.
If you want to use fwrite() then you will need to allocate the memory in the first argument so that it can read this and then write to the file. Check out malloc() and memset()
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.