LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 10-16-2005, 06:52 AM   #1
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Rep: Reputation: 32
C++: compiler returns an 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'

Last edited by frankie_DJ; 10-16-2005 at 07:20 AM.
 
Old 10-16-2005, 08:34 AM   #2
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
sp is a vector, and vectors don't have a "words" member.

Alex
 
Old 10-16-2005, 08:41 AM   #3
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Original Poster
Rep: Reputation: 32
Alex,

Can you be more specific? I have vector of type split_phrase. I thought that each of split_phrase objects in a vector has a member words. What am I missing?
 
Old 10-16-2005, 08:43 AM   #4
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
On the line with the error, you're not accessing a member of the 'sp' vector, you're just accessing the 'sp' vector (which itself has no 'words' member). Did you perhaps forget to index the vector before accessing the 'words' member?
 
Old 10-16-2005, 08:55 AM   #5
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Original Poster
Rep: Reputation: 32
I am sure you are right, but I am still not seeing it. If I am using push_back(), why do I neeed index?
 
Old 10-16-2005, 09:08 AM   #6
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
The problem is with
Code:
sp.words
sp is a vector, and it doesn't have a words member. Elements of sp do have a words member, so maybe you meant to index the vector and access the words member of one of its elements?

Alex
 
Old 10-16-2005, 09:15 AM   #7
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Original Poster
Rep: Reputation: 32
OK. I see it. Indexing sp will not work however. I have to start from scratch, this approach is a dead end.
 
  


Reply



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
checking for C compiler default output... configure: error: C compiler cannot create fiorejm Linux - Software 6 11-12-2009 12:35 PM
`su' returns error message anindyanuri Linux - Software 6 10-08-2005 05:00 PM
XConfigurator returns error yodathecoda Linux - Newbie 2 08-09-2004 04:07 PM
open function returns error sudheernair Programming 2 05-07-2004 11:14 AM
Configuration Error can't figure out!! amrogers3 Linux - Newbie 16 01-29-2004 12:36 PM

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

All times are GMT -5. The time now is 09:10 PM.

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