PDA

View Full Version : Coding conundrum (C++)




GeeYouEye
May 17, 2003, 01:12 AM
Why does this code segment

apvector<apstring> GetWords() // opens dictionary, reads words into a vector
{
ifstream fin("dictionary.txt")
apvector<apstring> vocab(0);
int counter = 0;
while (!fin.eof())
{
counter++;
getline(fin, vocab[counter-1]);
vocab.resize(counter);
}
fin.close();
return vocab;
}

generate these errors when compiled via the Terminal (using g++) (user name **'d)

/Users/**/Documents/AP Comp Sci Docs/Monkies.cpp: In function
`apvector<apstring> GetWords()':
/Users/**/Documents/AP Comp Sci Docs/Monkies.cpp:54: parse error before `
<' token
/Users/**/Documents/AP Comp Sci Docs/Monkies.cpp:59: `vocab' undeclared
(first use this function)
/Users/**/Documents/AP Comp Sci Docs/Monkies.cpp:59: (Each undeclared
identifier is reported only once for each function it appears in.)

I correctly #included "apvector.h" and "apstring.h" at the beginning, so I'm at a loss as to what's wrong.

Also, I have one other question, but I'll save it until later.

EDIT:
here's the syntax for apvector's use


// examples of use
// apvector<int> v1; // 0-element vector
// apvector<int> v2(4); // 4-element vector
// apvector<int> v3(4, 22); // 4-element vector, all elements == 22.


based on this constructor code

template <class itemType>
class apvector
{
public:

// constructors/destructor
apvector( ); // default constructor (size==0)
explicit apvector( int size ); // initial size of vector is size
apvector( int size, const itemType & fillValue ); // all entries == fillValue
apvector( const apvector & vec ); // copy constructor
~apvector( );


That all may help. Any ideas?



GeeYouEye
May 17, 2003, 01:22 AM
*sigh* never mind... for now... stupid semicolon... I still have one other problem, but I have a bit more correcting of the side-effects of the first fix to do first.

GeeYouEye
May 17, 2003, 01:48 AM
All right, that's all fixed. Only one error left, but it's a huge, (to me) completely incomprehensible one. And I think it has something to do with whatever the answer to this question is: Can you pass a ofstream to a function? The idea was that I can open the file for writing in one function, and then make a function call to output something to that file, and then later close the ofstream from the original function. If that makes any sense.

Here's the code segments in question (ignore the comments, they'll just confuse you even more):

void WriteStory(apvector<apstring> vocab)
{
ofstream fout("Story.txt"); //opens/creates Story.txt for writing
int i;
int stopAt = SentencesInStory();//same sit as before-can't repeat call
int para = SentencesInParagraph();
//similar but not same, as it will change. needed here to avoid declaration in
// for loop
int counter = 0; //sentences per paragraph counter
for (i=0; i < stopAt; i++)
{
if (counter < para)
{
SentenceOut(fout, vocab); //can I do this?
}
else
{
fout << endl << "\t";
para = SentencesInParagraph(); // the change
SentenceOut(fout, vocab); // goodness i hope so
}
}
fout << endl << endl << "The End"; //nice, and hope show where's wrong
fout.close();
}


void SentenceOut(ofstream fout, apvector<apstring> vocab)
{
int i;
int WB4P = WordsBeforePeriod();//needed to avoid repeated calls to it
for (i=0; i > WB4P; i++)
{
fout << vocab[WhichWordInVect(vocab)];
fout << " ";
}
fout << ". ";
}


And here's the error:

/usr/include/gcc/darwin/3.1/g++-v3/bits/ios_base.h: In copy constructor
`std::basic_ios<char, std::char_traits<char> >::basic_ios(const
std::basic_ios<char, std::char_traits<char> >&)':
/usr/include/gcc/darwin/3.1/g++-v3/bits/ios_base.h:421: `
std::ios_base::ios_base(const std::ios_base&)' is private
/Users/**/Documents/AP Comp Sci Docs/Monkies.cpp:97: within this context
/usr/include/gcc/darwin/3.1/g++-v3/streambuf: In copy constructor
`std::basic_filebuf<char, std::char_traits<char> >::basic_filebuf(const
std::basic_filebuf<char, std::char_traits<char> >&)':
/usr/include/gcc/darwin/3.1/g++-v3/streambuf:523: `std::basic_streambuf<_CharT,
_Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&)
[with _CharT = char, _Traits = std::char_traits<char>]' is private
/Users/**/Documents/AP Comp Sci Docs/Monkies.cpp:97: within this context


I've tried everything I can think of (including checking those pesky semicolons), but as I said, it's probably related to that question.

I hope I made myself understood here... it's nearly midnight, and I'm exhausted and not thinking too clearly.