error: invalid conversion from `void*' to `char**', where should I start to look for
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.
error: invalid conversion from `void*' to `char**', where should I start to look for
I'm doing my best to create functions for my program to read and write characters from a text file... just one small problem... on line 45 of the following code shown below, it cannot convert void* to char*. Here is the actual output with the g++ command:
Code:
[georig1\ 23:52:25\ george\ ~/cfiles/George Lair/050606]$ g++ TextINOUT.cpp -Wall -Wextra -o TextINOUT3
TextINOUT.cpp: In function `std::string* ReadOut()':
TextINOUT.cpp:45: error: invalid conversion from `void*' to `char**'
TextINOUT.cpp:45: error: cannot convert `std::string*' to `size_t*' for argument `2' to `__ssize_t getline(char**, size_t*, FILE*)'
[georig1\ 00:00:20\ george\ ~/cfiles/George Lair/050606]$
... here is the actual code that I made:
Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstdio>
using namespace std;
string * ReadOut(void); // This operates in conjunction with ReadFilesLines()
void WrightIn(string STRING); // This operates alone.
void ReadFileLines(void); // This operates in conjuction with ReadOut()
int main () {
string UserInput; // Data of UserInput goes to WrightIn()
string line;
cout << "\n\tWright something you want to put into a file, and I will display what you wrote.\n\n";
cout << "> ";
getline(cin,UserInput); cout << UserInput << " : Just to let you know that is what you wrote.\n"; // Input data into UserInput
WrightIn(UserInput); // Writes whatever the user inputed into UserInput into the example.txt file
ReadFileLines(); // Reads whatever the return value of ReadOut() is.
return 0;
}
void WrightIn(string STRING)
{
fstream infile ("example.txt");
if (infile.is_open())
{
infile << STRING;
infile.close();
}
else { cout << "Unable to open file"; }
}
string * ReadOut(void)
{
fstream outfile ("example.txt");
string * MESSAGE = new string;
if (outfile.is_open())
{
while (!outfile.eof())
{
getline(outfile,MESSAGE);
cout << "\nJust making sure this testPro is reaching this function.\n";
}
outfile.close();
}
else { cout << "Unable to open file"; }
return MESSAGE;
}
void ReadFileLines()
{
string * READ = ReadOut();
cout << "ReadFilesLines should be outputing the string READ from ReadOuts() return!\n\n"; cout << READ << endl;
delete READ; READ = NULL;
}
What exactly should I do to declare string * MESSAGE with whatever characters found in "example.txt"? If you find that it is cheating to give me the full answer, I have no problem with a push in the right direction, whatever that push maybe. Thank you in advance.
EDIT:
Since you're writing C++, you probably don' mean to use the regular getline() function. I assume you intended to use the getline member of an istream class. In that case, you need to invoke it as such--in other words, invoking it as a reference to an object (which I assume you intend "outfile").
Also, it's a little confusing, because getline is associated with an input stream--not an output stream. The variable names are a bit out-of-whack because you refer to the fstream as an "outfile" when you want to read from it.
EDIT-EDIT:
According to my reference book, the istream getline() method's prototype is:
istream &getline(char *buf, streamsize num);
Places num-1 characters into the buffer pointed to by buf.
Last edited by Dark_Helmet; 05-19-2006 at 03:44 AM.
I don't understand, the following works (found on cplusplus.com tutorials):
Code:
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
... but I read this when I look at the c++ reference to istream::getline():
Quote:
istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );
Get a line from stream.
Extracts characters from the stream and stores them into successive locations in the array pointed by s.
Characters are extracted until either (n - 1) characters have been extracted, the delimiter (parameter delim or '\n' if not specified) is found, or if the end of file or any error occurs in the input sequence.
If the delimiter is found it is extracted but not not stored. Use get if you don't want this character to be extracted.
An ending null character is automatically appended after the data stored in s.
Code:
TextINOUT.cpp:47: error: invalid conversion from `void*' to `size_t*'
is still what I get. But I don't get why I don't recieve the error from cplusplus's resource on getline and input output functions. It's reading from a source and storing it into a destination(getline (myfile/*source*/,line/*destination*/);, not extracting the source up until a certain amount of characters have been reached.
I have read man fopen, and I have tried to use it but with failure. Seems as though this is a c style way of reading from and writing a file.(stdio.h, stdlib.h, which in c++ is cstdio cstdlib) Here is the code provided with the man page of getline:
Code:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen("/etc/motd", "r");
if (fp == NULL)
exit(EXIT_FAILURE);
while ((read = getline(&line, &len, fp)) != -1) {
printf("Retrieved line of length %zu :\n", read);
printf("%s", line);
}
if (line)
free(line);
return EXIT_SUCCESS;
}
When you say invoke the istream member function getline, to me it means to call upon its object name, such as the following:
--PS--
If I misread something please point it out to me. Or correct me on something that I believe may be true... for I'm certain it's not. So I guess I don't believe it's true! :>
Last edited by RHLinuxGUY; 05-19-2006 at 11:48 PM.
I'm doing my best to create functions for my program to read and write characters from a text file... just one small problem... on line 45 of the following code shown below, it cannot convert void* to char*. Here is the actual output with the g++ command:
Code:
[georig1\ 23:52:25\ george\ ~/cfiles/George Lair/050606]$ g++ TextINOUT.cpp -Wall -Wextra -o TextINOUT3
TextINOUT.cpp: In function `std::string* ReadOut()':
TextINOUT.cpp:45: error: invalid conversion from `void*' to `char**'
TextINOUT.cpp:45: error: cannot convert `std::string*' to `size_t*' for argument `2' to `__ssize_t getline(char**, size_t*, FILE*)'
[georig1\ 00:00:20\ george\ ~/cfiles/George Lair/050606]$
... here is the actual code that I made:
Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstdio>
using namespace std;
string * ReadOut(void); // This operates in conjunction with ReadFilesLines()
void WrightIn(string STRING); // This operates alone.
void ReadFileLines(void); // This operates in conjuction with ReadOut()
int main () {
string UserInput; // Data of UserInput goes to WrightIn()
string line;
cout << "\n\tWright something you want to put into a file, and I will display what you wrote.\n\n";
cout << "> ";
getline(cin,UserInput); cout << UserInput << " : Just to let you know that is what you wrote.\n"; // Input data into UserInput
WrightIn(UserInput); // Writes whatever the user inputed into UserInput into the example.txt file
ReadFileLines(); // Reads whatever the return value of ReadOut() is.
return 0;
}
void WrightIn(string STRING)
{
fstream infile ("example.txt");
if (infile.is_open())
{
infile << STRING;
infile.close();
}
else { cout << "Unable to open file"; }
}
string * ReadOut(void)
{
fstream outfile ("example.txt");
string * MESSAGE = new string;
if (outfile.is_open())
{
while (!outfile.eof())
{
getline(outfile,MESSAGE);
cout << "\nJust making sure this testPro is reaching this function.\n";
}
outfile.close();
}
else { cout << "Unable to open file"; }
return MESSAGE;
}
void ReadFileLines()
{
string * READ = ReadOut();
cout << "ReadFilesLines should be outputing the string READ from ReadOuts() return!\n\n"; cout << READ << endl;
delete READ; READ = NULL;
}
What exactly should I do to declare string * MESSAGE with whatever characters found in "example.txt"? If you find that it is cheating to give me the full answer, I have no problem with a push in the right direction, whatever that push maybe. Thank you in advance.
getline() takes a string as its second argument. MESSAGE is a pointer to a string. That's why it does not work.
I fixed the problem with the help shown above. I kept thinking that I had to point MESSAGE to another object when I didn't need, thats how I understand the situation at least. Every instance that MESSAGE, or the variable that is made from the heap, is declared for input or output (excluding return), I needed to prefix MESSAGE with an asterisk. So the following works:
Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstdio>
using namespace std;
string * ReadOut(void); // This operates in conjunction with ReadFilesLines()
void WrightIn(string STRING); // This operates alone.
void ReadFileLines(void); // This operates in conjuction with ReadOut()
int main () {
string UserInput; // Data of UserInput goes to WrightIn()
string line;
cout << "\n\tWright something you want to put into a file, and I will display what you wrote.\n\n";
cout << "> ";
getline(cin,UserInput); cout << UserInput << " : Just to let you know that is what you wrote.\n"; // Input data into UserInput
WrightIn(UserInput); // Writes whatever the user inputed into UserInput into the example.txt file
ReadFileLines(); // Reads whatever the return value of ReadOut() is.
return 0;
}
void WrightIn(string STRING)
{
fstream infile ("example.txt");
if (infile.is_open())
{
infile << STRING;
infile.close();
}
else { cout << "Unable to open file"; }
}
string * ReadOut(void)
{
fstream outfile ("example.txt");
string * MESSAGE = new string;
if (outfile.is_open())
{
while (!outfile.eof())
{
getline(outfile,*MESSAGE);
cout << "\nJust making sure this testPro is reaching this function:1:.\n";
cout << *MESSAGE << endl;
}
outfile.close();
}
else { cout << "Unable to open file"; }
return MESSAGE;
}
void ReadFileLines()
{
string * READ = ReadOut();
cout << "ReadFilesLines should be outputing the string READ from ReadOuts() return!\n\n"; cout << *READ << endl;
delete READ; READ = NULL;
}
Last edited by RHLinuxGUY; 05-20-2006 at 08:54 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.