Compiler returns error, can't figure out why

F

Frankie Montenegro

Hi everyone,

I need some help with the following C++ code. It is supposed to read input
line by line, each line as one string; then split each string into its
component words and store these components in a vector<string> (so it
would be a vector of strings per line)
('Split' function mentioned in the code is defined elsewhere and it workes
fine; it splits a line of input into words and stores them in
vector<string>.)

code:

#include <iostream>
#include <string>
#include <vector>
#include "split.h"
//using declarations

struct split_phrase
{
vector<string> words;
};

int main()
{
string s;
vector<string> phrase;
vector<split_phrase> sp;
while (getline(cin,s))
phrase.push_back(s);
for (vector<string>::size_type i=0; i!=phrase.size(); ++i)
ERROR HERE! (sp.words).push_back(split(phrase));

cout<<sp[2].words[3];
return 0;
}


Below is the only error returned. I don't understand why it shows. ANy thougths?

code:

5-1.cc: In function `int main()':
 
G

guyarad

ok, you are not using the vector correctly.
the var _sp_ is a vector of split_phrase. sp has no member named words!
each cell of the vector has a member name words, but not sp.
my guess is that _sp_ intended to hold an element for each input line,
and each element (which is a split_phrase struct) should hold a vector
of strings, which will represent the words.
at first this (sp) vector is empty.
for each line of input, or in that case for each element in phrase,
you should create a new element in sp (using push_back) and then insert
the phrase into it.
or vice versa, create a split_phrase instance. insert the splited
vector into it,
and the push back in sp.
although i'm not sure what the function split returns i'll assume it's
a vector.
if not, you should alter the code a bit:
for (vector<string>::size_type i=0; i!=phrase.size(); ++i)
{
split_phrase tmp;
tmp.words = split(phrase);
sp.push_back(tmp);
}
 
F

Frankie Montenegro

guyarad
thanks for the answer. you were right about the use of
vectors. I tried a different apprach this time. this one
seems to make more sense, but it still returns the errors I
can't explain.
Here it is:

struct split_phrase
{
string phrase;
vector<string> words;
};
//ERROR IS IN THE LINE BELOW!
istream& read_phrase(istream& in, split_phrase& sp)
{
getline(in, sp.phrase);
sp.words=split(sp.phrase);
return in;
}

int main()
{
split_phrase s;
vector<split_phrase> vsp;
while (read_phrase(cin,s))
vsp.push_back(s);
cout<<vsp[2].words[3];
return 0;
}
I get the error in the first and fourth line of read_phrase and then
of course everywhere I mention that function. Can't figure
out why.

5-1.cc:38: error: syntax error before `&' token
5-1.cc:41: error: syntax error before `.' token
5-1.cc: In function `int main()':
5-1.cc:49: error: `read_phrase' undeclared (first use this
function)
5-1.cc:49: error: (Each undeclared identifier is reported
only once for each
function it appears in.)
 
J

Jim Langston

Frankie Montenegro said:
guyarad
thanks for the answer. you were right about the use of
vectors. I tried a different apprach this time. this one
seems to make more sense, but it still returns the errors I
can't explain.
Here it is:

struct split_phrase
{
string phrase;
vector<string> words;
};
//ERROR IS IN THE LINE BELOW!
istream& read_phrase(istream& in, split_phrase& sp)

Did you #include said:
{
getline(in, sp.phrase);
sp.words=split(sp.phrase);
return in;
}

int main()
{
split_phrase s;
vector<split_phrase> vsp;
while (read_phrase(cin,s))
vsp.push_back(s);
cout<<vsp[2].words[3];
return 0;
}
I get the error in the first and fourth line of read_phrase and then
of course everywhere I mention that function. Can't figure
out why.

5-1.cc:38: error: syntax error before `&' token
5-1.cc:41: error: syntax error before `.' token
5-1.cc: In function `int main()':
5-1.cc:49: error: `read_phrase' undeclared (first use this
function)
5-1.cc:49: error: (Each undeclared identifier is reported
only once for each
function it appears in.)
 
M

Mike Wahler

Frankie Montenegro said:
guyarad
thanks for the answer. you were right about the use of
vectors. I tried a different apprach this time. this one
seems to make more sense, but it still returns the errors I
can't explain.
Here it is:

#include <istream>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
struct split_phrase
{
string phrase;
vector<string> words;
};
//ERROR IS IN THE LINE BELOW!
istream& read_phrase(istream& in, split_phrase& sp)
{
getline(in, sp.phrase);
sp.words=split(sp.phrase);

I don't see any definition or prototype for function
'split()'.

-Mike
 
J

Jim Langston

Jim Langston said:
Frankie Montenegro said:
guyarad
thanks for the answer. you were right about the use of
vectors. I tried a different apprach this time. this one
seems to make more sense, but it still returns the errors I
can't explain.
Here it is:

struct split_phrase
{
string phrase;
vector<string> words;
};
//ERROR IS IN THE LINE BELOW!
istream& read_phrase(istream& in, split_phrase& sp)

Did you #include said:
{
getline(in, sp.phrase);
sp.words=split(sp.phrase);
return in;
}

int main()
{
split_phrase s;
vector<split_phrase> vsp;
while (read_phrase(cin,s))
vsp.push_back(s);
cout<<vsp[2].words[3];
return 0;
}
I get the error in the first and fourth line of read_phrase and then
of course everywhere I mention that function. Can't figure
out why.

5-1.cc:38: error: syntax error before `&' token
5-1.cc:41: error: syntax error before `.' token
5-1.cc: In function `int main()':
5-1.cc:49: error: `read_phrase' undeclared (first use this
function)
5-1.cc:49: error: (Each undeclared identifier is reported
only once for each
function it appears in.)

The following compiles for me. Since you didn't give the code for split I
just return a vector of std::string. If your's is not compiling it must be
somewhere in code you are not showing us. Note I just said it compiles, I
didn't try to run it.

#include <iostream>
#include <string>
#include <vector>

std::vector<std::string> split( std::string phrase )
{
std::vector<std::string> returnVector;
return returnVector;
}

struct split_phrase
{
std::string phrase;
std::vector<std::string> words;
};

std::istream& read_phrase(std::istream& in, split_phrase& sp)
{
std::getline(in, sp.phrase);
sp.words=split(sp.phrase);
return in;
}

int main()
{
split_phrase s;
std::vector<split_phrase> vsp;
while (read_phrase(std::cin,s))
vsp.push_back(s);
std::cout<<vsp[2].words[3];
return 0;
}
 

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
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top