473,387 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Compiler returns error, can't figure out why

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[i]));

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()':
5-1.cc:45: error: 'class std::vector<split_phrase, std::allocator<split_phrase>
' has no member named 'words'

Oct 16 '05 #1
6 1768
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[i]);
sp.push_back(tmp);
}

Oct 16 '05 #2
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.)
Oct 17 '05 #3

"Frankie Montenegro" <ma******@comcast.net> wrote in message
news:pa***************************@comcast.net...
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 <istream> (or is that <ifstream>)?
{
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.)

Oct 17 '05 #4
<iostream> is #included, yes.
Oct 17 '05 #5

"Frankie Montenegro" <ma******@comcast.net> wrote in message
news:pa***************************@comcast.net...
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
Oct 17 '05 #6

"Jim Langston" <ta*******@rocketmail.com> wrote in message
news:4H*******************@fe05.lga...

"Frankie Montenegro" <ma******@comcast.net> wrote in message
news:pa***************************@comcast.net...
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 <istream> (or is that <ifstream>)?
{
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;
}
Oct 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mark A. Washburn | last post by:
/* Test4 WARNING THIS PROGRAM NEVER ENDS DUE TO JAVA COMPILER ERROR Neither Sun JDK 1.1.8 nor JDK 1.4.1 compiler will output *any* error or warning A disassembly of the class file shows...
30
by: Neil Zanella | last post by:
Hello, Allow me to share my frustrations with GNU g++. This is the second time something similar happens to me: I can't find anything wrong with my C++ program and yet I get segfaults, and...
13
by: Neil Zanella | last post by:
Hello, I wonder whether anyone has ever come across the following g++ compiler error message. I don't recall ever seeing it before. I solved my problem but I am still not sure about what this...
5
by: Lou Pecora | last post by:
g++ compiler error question. I have a container C whose constructor takes a class B that is inherited from an abstract class A. So I have the line of code: B binstance; C ...
13
by: Andrew | last post by:
I use conditional compiler constants, set through the VBA IDE in Tools, <projectname> Properties, that I refer to throughout my code to control which code is used during development, and which...
102
by: Skybuck Flying | last post by:
Sometime ago on the comp.lang.c, I saw a teacher's post asking why C compilers produce so many error messages as soon as a closing bracket is missing. The response was simply because the compiler...
5
by: dilip ranganathan | last post by:
Hi I have taken the liberty to cross-post this. It appeared on c.l.c++.m but the ICE is regarding VS.NET 7.1 C++ compiler. post follows: ==============================================...
9
by: JTrigger | last post by:
When I compile my project using the IDE on a development machine it works just fine. When I compile it on the server using csc.exe, I get the following error when I try to bring it up in the web...
0
by: erik.erikson | last post by:
I am getting a compiler error that I can't well explain or even understand the origin of (though I boiled it down close...). Below is a bare-bones example. What I am doing is defining the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.