Witam serdecznie!
Po długiej przerwie znów siadam do Cpp i już od razu kompilator musi pokazać, że jest mądrzejszy.
Fragment treści zadania(bodajże maturalne):
W pliku dane.txt w oddzielnych wierszach znajdują się słowa o długościach od 2 do 20 znaków, składające się z wielkich liter A, B, C, D, E, F. Odpowiedzi do poniższych podpunktów umieść w pliku tekstowym wyniki.txt. Odpowiedź do każdego podpunktu poprzedź literą oznaczającą ten podpunkt.
a) W pliku wyniki.txt podaj w osobnych wierszach:
- ile jest słów w pliku dane.txt występujących więcej niż jeden raz,
- słowo o największej liczbie wystąpień,
- liczbę jego wystąpień.
.
Pomyślałem, że żeby oszczędzić kodu i czasu zrobię to vectorem ze strukturą wewnątrz. Znalazłem pewne magiczne podpowiedzi, ale to nie wiele dało:
http://stackoverflow.com/questions/8067338/c-vector-of-structs-initializationPoniżej zamieszczam kod, a w komentarzach errory z Eclipsa... Okej. Nie zamieszczam komentarzy z errorami, bo to nie wygląda na czytelne...:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
ifstream plik( "dane.txt" );
ofstream wynik( "wyniki.txt" );
string slowo;
struct zestaw {
string slowo;
unsigned int ilosc;
};
vector < zestaw > tab;
bool spr;
int a1, a2;
if( plik.is_open() )
while( !plik.eof() )
{
spr = false;
getline( plik, slowo );
if( tab.empty() )
{
tab.push_back( zestaw() );
tab[ 0 ].slowo = slowo;
tab[ 0 ].ilosc = 1;
}
else
{
for( unsigned int i = 0; i < tab.size(); i++ )
{
if( tab[ i ].slowo == slowo )
{
tab[ i ].ilosc++;
spr = true;
}
}
if( !spr )
{
tab.push_back( zestaw() );
tab[ tab.size() - 1 ].slowo = slowo;
tab[ tab.size() - 1 ].ilosc = 1;
}
}
}
else
cout << "Problem z plikiem!" << endl;
for( unsigned int i = 0; i < tab.size(); i++ )
{
a1 = 0;
a2 = 0;
if( tab[ i ].ilosc > 1 )
{
a1++;
if( tab[ i ].ilosc > tab[ a2 ].ilosc )
a2 = i;
}
}
wynik << "a)\t " << a1 << "\n";
wynik << "\t " << tab[ a2 ].slowo << "\n";
wynik << "\t " << tab[ a2 ].ilosc << "\n";
plik.close();
wynik.close();
return 0;
}
Oo... Nie zauważyłem, że mi konsola coś wypluła. :) Proszę:
22:19:47 **** Incremental Build of configuration Debug for project MoodlAlgorytmika01 ****
make all
Building file: ../main.cpp
Invoking: Cross G++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "../main.cpp"
../main.cpp: In function ‘int main()’:
../main.cpp:17:15: error: template argument for ‘template<class> class std::allocator’ uses local type ‘main()::zestaw’
vector<zestaw> tab;
^
../main.cpp:17:15: error: trying to instantiate ‘template<class> class std::allocator’
../main.cpp:17:15: error: template argument 2 is invalid
../main.cpp:17:20: error: invalid type in declaration before ‘;’ token
vector<zestaw> tab;
^
../main.cpp:26:11: error: request for member ‘empty’ in ‘tab’, which is of non-class type ‘int’
if(tab.empty())
^
../main.cpp:28:9: error: request for member ‘push_back’ in ‘tab’, which is of non-class type ‘int’
tab.push_back(zestaw());
^
../main.cpp:29:10: error: invalid types ‘int[int]’ for array subscript
tab[0].slowo = slowo;
^
../main.cpp:30:10: error: invalid types ‘int[int]’ for array subscript
tab[0].ilosc = 1;
^
../main.cpp:34:37: error: request for member ‘size’ in ‘tab’, which is of non-class type ‘int’
for(unsigned int i = 0; i < tab.size(); i++)
^
../main.cpp:36:14: error: invalid types ‘int[unsigned int]’ for array subscript
if(tab[i].slowo == slowo)
^
../main.cpp:38:12: error: invalid types ‘int[unsigned int]’ for array subscript
tab[i].ilosc++;
^
../main.cpp:44:10: error: request for member ‘push_back’ in ‘tab’, which is of non-class type ‘int’
tab.push_back(zestaw());
^
../main.cpp:45:14: error: request for member ‘size’ in ‘tab’, which is of non-class type ‘int’
tab[tab.size()-1].slowo = slowo;
^
../main.cpp:46:14: error: request for member ‘size’ in ‘tab’, which is of non-class type ‘int’
tab[tab.size()-1].ilosc = 1;
^
../main.cpp:53:34: error: request for member ‘size’ in ‘tab’, which is of non-class type ‘int’
for(unsigned int i = 0; i < tab.size(); i++)
^
../main.cpp:58:11: error: invalid types ‘int[unsigned int]’ for array subscript
if(tab[i].ilosc > 1)
^
../main.cpp:61:12: error: invalid types ‘int[unsigned int]’ for array subscript
if(tab[i].ilosc > tab[a2].ilosc)
^
../main.cpp:61:28: error: invalid types ‘int[int]’ for array subscript
if(tab[i].ilosc > tab[a2].ilosc)
^
../main.cpp:67:26: error: invalid types ‘int[int]’ for array subscript
wynik << "\t " << tab[a2].slowo << "\n";
^
../main.cpp:68:26: error: invalid types ‘int[int]’ for array subscript
wynik << "\t " << tab[a2].ilosc << "\n";
^
make: *** [main.o] Error 1
22:19:47 Build Finished (took 536ms)
Bardzo proszę o wskazanie błędu. Ciekawi mnie co nie tak, bo to wygląda dość logicznie... Dość.