Compiling problems with g++

A

Arturo DiDonna

Hello everyone. I am trying to compile someone else code and I am
stuck with compilation problems using
the g++ 3.3 compiler. Basically, when compiling the following code, I
get this error message:

parsefcns.cc: In function `void get_token(std::ifstream*, char**)':
parsefcns.cc:57: error: cannot convert `std::basic_string<char,
std::char_traits<char>, std::allocator<char> >' to `char*' in
assignment
make: *** [parsefcns.o] Error 1

I don't know C++ and I was wondering if there was a kind soul willing
to help me in solving this problem.
Thanks in advance.
Arturo

...........................................
#include <fstream>
#include <sstream>
#include <cctype>
#include <cstring>
#include <iostream>

using namespace std;
using std::ifstream;
using std::eek:stringstream;

void get_token(std::ifstream *f_stream, char **ch_ptr)
{
char ch;
std::eek:stringstream buffer;

while( (f_stream->get(ch)) &&
(!isspace(ch)) &&
(!f_stream->eof()) )
{
buffer << ch;
}
buffer << '\0';
(*ch_ptr) = buffer.str(); // This is the line giving compilation
problems
// return ch;
f_stream->seekg(-1, std::ios::cur);
}
.............etc.
 
M

Mike Wahler

Arturo DiDonna said:
Hello everyone. I am trying to compile someone else code and I am
stuck with compilation problems using
the g++ 3.3 compiler. Basically, when compiling the following code, I
get this error message:

parsefcns.cc: In function `void get_token(std::ifstream*, char**)':
parsefcns.cc:57: error: cannot convert `std::basic_string<char,
std::char_traits<char>, std::allocator<char> >' to `char*' in
assignment
make: *** [parsefcns.o] Error 1

I don't know C++ and I was wondering if there was a kind soul willing
to help me in solving this problem.
Thanks in advance.
Arturo

..........................................
#include <fstream>
#include <sstream>
#include <cctype>
#include <cstring>
#include <iostream>

using namespace std;

This directive..
using std::ifstream;
using std::eek:stringstream;

... obviates the need for these two declarations.
void get_token(std::ifstream *f_stream, char **ch_ptr)

const char **ch_ptr
{
char ch;
std::eek:stringstream buffer;

while( (f_stream->get(ch)) &&
(!isspace(ch)) &&
(!f_stream->eof()) )
{
buffer << ch;
}
buffer << '\0';
(*ch_ptr) = buffer.str(); // This is the line giving compilation
problems

*ch_ptr = buffer.str().c_str();
// return ch;
f_stream->seekg(-1, std::ios::cur);

Better check for success/failure here.

}
............etc.

-Mike
 
A

Andrey Tarasevich

Arturo said:
...
void get_token(std::ifstream *f_stream, char **ch_ptr)
{
char ch;
std::eek:stringstream buffer;

while( (f_stream->get(ch)) &&
(!isspace(ch)) &&
(!f_stream->eof()) )
{
buffer << ch;
}
buffer << '\0';
(*ch_ptr) = buffer.str(); // This is the line giving compilation
problems

Return type of 'std::eek:stringstream::str()' is 'std::string' and you are
trying to assign this value to an object of type 'char*'. Obviously,
this is an error.

Anyway, this can't work. It appears that you are trying to return a
pointer to an object that will be destroyed when function exits. What
are you trying to implement?
 
J

John Harrison

Arturo DiDonna said:
Hello everyone. I am trying to compile someone else code and I am
stuck with compilation problems using
the g++ 3.3 compiler. Basically, when compiling the following code, I
get this error message:

parsefcns.cc: In function `void get_token(std::ifstream*, char**)':
parsefcns.cc:57: error: cannot convert `std::basic_string<char,
std::char_traits<char>, std::allocator<char> >' to `char*' in
assignment
make: *** [parsefcns.o] Error 1

I don't know C++ and I was wondering if there was a kind soul willing
to help me in solving this problem.
Thanks in advance.
Arturo

..........................................
#include <fstream>
#include <sstream>
#include <cctype>
#include <cstring>
#include <iostream>

using namespace std;
using std::ifstream;
using std::eek:stringstream;

void get_token(std::ifstream *f_stream, char **ch_ptr)
{
char ch;
std::eek:stringstream buffer;

while( (f_stream->get(ch)) &&
(!isspace(ch)) &&
(!f_stream->eof()) )
{
buffer << ch;
}
buffer << '\0';
(*ch_ptr) = buffer.str(); // This is the line giving compilation
problems
// return ch;
f_stream->seekg(-1, std::ios::cur);
}
............etc.

How did you manage all this code without knowing C++?

Anyway the code is wrong but what the fix is isn't clear, and since you
don't know C++ you can help either. At a guess I would say that the code is
meant to use ostrstream not ostringstream, and <strstream> not <sstream>,
that would make it compile at least I think.

john
 
T

teahouse todd

while( (f_stream->get(ch)) &&
(!isspace(ch)) &&
(!f_stream->eof()) )
Your code here has problems, should be

while( (f_stream->get(ch)) &&
(!isspace(ch)) )
(*ch_ptr) = buffer.str(); // This is the line giving compilation
Shoudle be

(*ch_ptr) == buffer.str().c_str();
 
T

teahouse todd

(*ch_ptr) = buffer.str(); // This is the line giving compilation
After I post the replay as

I got some feeling:
1) buffer is "stack", so buffer.str() is "stack"
2) convert const char* --> char*
So the programming is so error-prone, you never need to fixed it.
Just kick it out,
and find some good one....
 
D

Dylan Nicholson

Mike Wahler said:
*ch_ptr = buffer.str().c_str();
How can that possibly produce a useful result? 'ch_ptr' is obviously
an output parameter, but the buffer will no longer exist after this
function returns.
But I can't even begin to guess what it should be...other than
something fairly questionable like:

*ch_ptr = strdup(buffer.str().c_str());

Dylan
 
J

John Harrison

teahouse todd said:
Your code here has problems, should be

while( (f_stream->get(ch)) &&
(!isspace(ch)) )

Shoudle be

(*ch_ptr) == buffer.str().c_str();

That doesn't work. It might make the code compile but it will surely crash
when it runs since the memory pointed to by *ch_ptr will have been deleted.

john
 
A

Arturo DiDonna

Many thanks to all of you who answered my question. I tried first Mike
Wahler suggestion and it worked like charm! Thanks again, Mike.
Unfortunately, the compiler moved past this error just to find some
more... Here is the error message I get now from the following code :

parameters.cc: In constructor `Parameters::parameters()':
parameters.cc:99: error: cannot convert `std::basic_string<char,
std::char_traits<char>, std::allocator<char> >' to `char*' in
assignment
make: *** [parameters.o] Error 1

#include <cstdlib>
#include <stdio.h>
#include <cctype>
#include <fstream>
#include <cstring>
#include <sstream>

#include "parameters.h"
#include "parsefcns.h"
#include "dlListADT.h"
#include "dlListADT.cc"

using namespace std;

Parameters::parameters() {
int i;

patt_len = 10;
alph_size = 4;
min_wt = -2.5;
max_wt = 2.5;
step = 3.0;
decay = 0.9417;
momentum = 0.0;

// &&& Begin: Initialization of parameters for second site.
patt_len2 = 10;
initial_wts_factor2 = 0.5;
min_wt2 = -2.5;
max_wt2 = 2.5;
step2 = 3.0;
decay2 = 0.9417;
momentum2 = 0.0;
begin_word2 = NULL;
data2_file = NULL;
weights_file2 = NULL;
energy_file2 = NULL;
max_dist_bw_sites = 0;
search_direction = 0;
// &&& End: Initialization of parameters for second site.

num_sites_sampled = 10000;
sites_per_seq = 1;
comp_flag = 0;
clip_flag = 1;
format_spec = 0;
iters = 2000;
report_interval = 200;
initial_wts_factor = 0.5;
training_runs = 1;
random_num_seed = 17;
partition_option = 1;
begin_word = NULL;
alphabet_file = NULL;
data1_file = NULL; // manditory file
weights_file = NULL;
energy_file = NULL;
param_file = NULL;


std::eek:stringstream buffer;

usage = buffer.str(); // This line is giving the error message

registry = new int[SWITCHES];
for(i=0;i<SWITCHES;i++) registry = 0;
}
...................................

To make matter even worse, if I comment out the previous error line, I
get another error message from the compiler.

sequenceset.cc: In member function `virtual int
SeqFileReaderObj::read_chunk_of_sequence(int, std::ifstream*,
char**)':
sequenceset.cc:923: error: `pcount' undeclared (first use this
function)
sequenceset.cc:923: error: (Each undeclared identifier is reported
only once
for each function it appears in.)

I report only the code snippet giving the problem (I hope it will
suffice):

........................
int SeqFileReaderObj::read_chunk_of_sequence(int size, std::ifstream
*f_stream,
char **ch_ptr)
{
char ch;
std::eek:stringstream buffer;

while((f_stream->get(ch)) && (ch != Seq_Delim) && (buffer.pcount() <
size) ) //This line generated the compiler error


...................

Thanks again for your help.
Arturo

P.S. I wasn't very clear in my first message. This is not my code, as
I don't know C++. It is a code from someone else that compiled fine on
Solaris (g++ 2.95) but didn't on Linux Mandrake 9.1 (g++ 3.3). I tried
myself to hack some changes according to the compiler error/warning
messages and to what I read on the net, i.e., I replaced 'strstream.h'
with 'sstream', 'ostrstream' with 'ostringstream', 'stdio.h' with
'cstdio', but, as you see, I was still not very successful in
compiling the program...




Mike Wahler said:
Arturo DiDonna said:
Hello everyone. I am trying to compile someone else code and I am
stuck with compilation problems using
the g++ 3.3 compiler. Basically, when compiling the following code, I
get this error message:

parsefcns.cc: In function `void get_token(std::ifstream*, char**)':
parsefcns.cc:57: error: cannot convert `std::basic_string<char,
std::char_traits<char>, std::allocator<char> >' to `char*' in
assignment
make: *** [parsefcns.o] Error 1

I don't know C++ and I was wondering if there was a kind soul willing
to help me in solving this problem.
Thanks in advance.
Arturo

..........................................
#include <fstream>
#include <sstream>
#include <cctype>
#include <cstring>
#include <iostream>

using namespace std;

This directive..
using std::ifstream;
using std::eek:stringstream;

.. obviates the need for these two declarations.
void get_token(std::ifstream *f_stream, char **ch_ptr)

const char **ch_ptr
{
char ch;
std::eek:stringstream buffer;

while( (f_stream->get(ch)) &&
(!isspace(ch)) &&
(!f_stream->eof()) )
{
buffer << ch;
}
buffer << '\0';
(*ch_ptr) = buffer.str(); // This is the line giving compilation
problems

*ch_ptr = buffer.str().c_str();
// return ch;
f_stream->seekg(-1, std::ios::cur);

Better check for success/failure here.

}
............etc.

-Mike
 
A

Andrey Tarasevich

Arturo said:
Many thanks to all of you who answered my question. I tried first Mike
Wahler suggestion and it worked like charm! Thanks again, Mike.

Note, that even though Mike's suggestion will help that piece of code to
_compile_, it still won't make it _work_. As many people already pointed
out, the code looks like an incomplete attempt to switch from using
'strstream's to using 'stringstream's. The truth is that in your case it
is not enough to just replace all 'strstream' declarations with
'stringstream' declarations and subdue any compilation errors. In its
current state the code will return a pointer to deallocated memory,
which is a serious error.

The original 'strstream'-based version would've worked. The current
'stringstream'-based one will not work because these stream classes use
very different approaches to internal memory management.
 
M

Mike Wahler

Dylan Nicholson said:
*ch_ptr = buffer.str().c_str();
How can that possibly produce a useful result?[/QUOTE]

I didn't say it does. I did not look at the 'logic' (or
lack of) of the code. I simply addressed the specific
error message about the 'type conflict'.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,163
Messages
2,570,897
Members
47,436
Latest member
MaxD

Latest Threads

Top