LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-18-2010, 10:21 PM   #1
Kenny_Strawn
Senior Member
 
Registered: Feb 2010
Location: /usa/ca/orange_county/lake_forest
Distribution: ArchBang, Google Android 2.1 + Motoblur (on Motortola Flipside), Google Chrome OS (on Cr-48)
Posts: 1,791
Blog Entries: 62

Rep: Reputation: 56
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?
 
Old 09-18-2010, 10:24 PM   #2
Kenny_Strawn
Senior Member
 
Registered: Feb 2010
Location: /usa/ca/orange_county/lake_forest
Distribution: ArchBang, Google Android 2.1 + Motoblur (on Motortola Flipside), Google Chrome OS (on Cr-48)
Posts: 1,791

Original Poster
Blog Entries: 62

Rep: Reputation: 56
I Googled this error (in quotes, of course) and it returned no results.
 
Old 09-19-2010, 06:47 AM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Code:
cin >> null_char*bs*count >> image_file;
Did you mean to use cout here instead?
 
Old 09-19-2010, 08:04 AM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by ntubski View Post
Code:
cin >> null_char*bs*count >> image_file;
Did you mean to use cout here instead?
cout would also make no sense.

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.

Last edited by johnsfine; 09-19-2010 at 08:08 AM.
 
Old 09-19-2010, 09:38 AM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Kenny_Strawn View Post
Any idea what that error in the title means?

Here is my code:

Code:
...
    cin >> null_char*bs*count >> image_file;
...
Is the problem that I cannot cin the same value twice or something? If so, how do I get around the problem?
Is 'cin >>' supposed to read from stdin and put the data into an expression ?

I.e. what does formal 'cin' description say regarding items into which data can be read from stdin ?
 
Old 09-19-2010, 10:48 AM   #6
Kenny_Strawn
Senior Member
 
Registered: Feb 2010
Location: /usa/ca/orange_county/lake_forest
Distribution: ArchBang, Google Android 2.1 + Motoblur (on Motortola Flipside), Google Chrome OS (on Cr-48)
Posts: 1,791

Original Poster
Blog Entries: 62

Rep: Reputation: 56
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.
Makes sense now?
 
Old 09-19-2010, 11:21 AM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Kenny_Strawn View Post
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.
 
Old 09-19-2010, 11:28 AM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
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.
 
Old 09-19-2010, 11:53 AM   #9
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by Kenny_Strawn View Post
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.
 
Old 09-19-2010, 04:56 PM   #10
Kenny_Strawn
Senior Member
 
Registered: Feb 2010
Location: /usa/ca/orange_county/lake_forest
Distribution: ArchBang, Google Android 2.1 + Motoblur (on Motortola Flipside), Google Chrome OS (on Cr-48)
Posts: 1,791

Original Poster
Blog Entries: 62

Rep: Reputation: 56
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.
 
Old 09-19-2010, 05:46 PM   #11
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by johnsfine View Post
cout would also make no sense.

...

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.
 
Old 09-19-2010, 08:11 PM   #12
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by Kenny_Strawn View Post
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.
 
Old 09-19-2010, 08:29 PM   #13
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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 View Post
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
 
Old 09-19-2010, 09:03 PM   #14
Kenny_Strawn
Senior Member
 
Registered: Feb 2010
Location: /usa/ca/orange_county/lake_forest
Distribution: ArchBang, Google Android 2.1 + Motoblur (on Motortola Flipside), Google Chrome OS (on Cr-48)
Posts: 1,791

Original Poster
Blog Entries: 62

Rep: Reputation: 56
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.
 
Old 09-19-2010, 09:14 PM   #15
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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()
 
  


Reply

Tags
cin gcc+error



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++. cin does not wait for an input by the operator iqbala Programming 7 01-11-2010 12:32 PM
Overload Cast Operator in C++ halfpower Programming 2 10-18-2007 08:46 PM
Nested Overload << operator Mistro116@yahoo.com Programming 6 10-09-2006 09:56 AM
C++::std::cin adilturbo Programming 3 07-03-2006 11:22 AM
how to overload operator! jhorvath Programming 2 10-31-2003 07:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration