在VC中调试c++ primer的例子程序的问题,请高手指点??

redwolf310 2005-04-15 07:38:39
在VC中调试c++ primer的例子程序遇到问题,请高手指点:
-------------------tquery.h-------------------------------
namespace lin_kai
{

typedef pair<short,short> location;
typedef vector<location,allocator> loc;
typedef vector<string,allocator> text;
typedef pair<text*,loc*> text_loc;

class TextQuery {
public:
TextQuery() { memset( this, 0, sizeof( TextQuery )); }

static void filter_elements( string felems ) { filt_elems = felems; }

void query_text();
void display_map_text();
void display_text_locations();
void doit() {
retrieve_text();
separate_words();
filter_text();
suffix_text();
strip_caps();
build_word_map();
}

private:
void retrieve_text();
void separate_words();
void filter_text();
void strip_caps();
void suffix_text();
void suffix_s( string& );
void build_word_map();

private:
vector<string,allocator> *lines_of_text;
text_loc *text_locations;
map<string,loc*,less<string>,allocator> *word_map;
static string filt_elems;
};

string TextQuery::filt_elems( "\",.;:!?)(\\/" );

void
TextQuery::
retrieve_text()
{
string file_name;

cout << "please enter file name: ";
cin >> file_name;

ifstream infile( file_name.c_str(), ios::in );
if ( !infile ) {
cerr << "oops! unable to open file "
<< file_name << " -- bailing out!\n";
exit( -1 );
}
else cout << "\n";

lines_of_text = new vector<string,allocator>;
string textline;

while ( getline( infile, textline, '\n' ))
lines_of_text->push_back( textline );
}

void
TextQuery::
separate_words()
{
vector<string,allocator> *words = new vector<string,allocator>;
vector<location,allocator> *locations = new vector<location,allocator>;

for ( short line_pos = 0; line_pos < lines_of_text->size(); line_pos++ )
{
short word_pos = 0;
string textline = (*lines_of_text)[ line_pos ];

string::size_type eol = textline.length();
string::size_type pos = 0, prev_pos = 0;

while (( pos = textline.find_first_of( ' ', pos )) != string::npos )
{
words->push_back( textline.substr( prev_pos, pos - prev_pos ));
locations->push_back( make_pair( line_pos, word_pos ));

word_pos++; pos++; prev_pos = pos;
}

words->push_back( textline.substr( prev_pos, pos - prev_pos ));
locations->push_back( make_pair( line_pos, word_pos ));
}

text_locations = new text_loc( words, locations );
}

void
TextQuery::
filter_text()
{
if ( filt_elems.empty() )
return;

vector<string,allocator> *words = text_locations->first;

vector<string,allocator>::iterator iter = words->begin();
vector<string,allocator>::iterator iter_end = words->end();

while ( iter != iter_end )
{
string::size_type pos = 0;
while (( pos = (*iter).find_first_of( filt_elems, pos )) != string::npos )
(*iter).erase(pos,1);
iter++;
}
}

void
TextQuery::
suffix_text()
{
vector<string,allocator> *words = text_locations->first;

vector<string,allocator>::iterator iter = words->begin();
vector<string,allocator>::iterator iter_end = words->end();

while ( iter != iter_end )
{
// if 3 or less characters, let it be
if ( (*iter).size() <= 3 ) { iter++; continue; }
if ( (*iter)[ (*iter).size()-1 ] == 's' )
suffix_s( *iter );

// additional suffix handling goes here ...

iter++;
}
}

void
TextQuery::
suffix_s( string &word )
{
string::size_type spos = 0;
string::size_type pos3 = word.size()-3;

// "ous", "ss", "is", "ius"
string suffixes( "oussisius" );

if ( ! word.compare( pos3, 3, suffixes, spos, 3 ) ||
! word.compare( pos3, 3, suffixes, spos+6, 3 ) ||
! word.compare( pos3+1, 2, suffixes, spos+2, 2 ) ||
! word.compare( pos3+1, 2, suffixes, spos+4, 2 ))
return;

string ies( "ies" );
if ( ! word.compare( pos3, 3, ies ))
{
word.replace( pos3, 3, 1, 'y' );
return;
}

string ses( "ses" );
if ( ! word.compare( pos3, 3, ses ))
{
word.erase( pos3+1, 2 );
return;
}

// erase ending 's'
word.erase( pos3+2 );

// watch out for "'s"
if ( word[ pos3+1 ] == '\'' )
word.erase( pos3+1 );
}

void
TextQuery::
strip_caps()
{
vector<string,allocator> *words = text_locations->first;

vector<string,allocator>::iterator iter = words->begin();
vector<string,allocator>::iterator iter_end = words->end();

string caps( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );

while ( iter != iter_end ) {
string::size_type pos = 0;
while (( pos = (*iter).find_first_of( caps, pos )) != string::npos )
(*iter)[ pos ] = tolower( (*iter)[pos] );
++iter;
}
}


void
TextQuery::
build_word_map()
{
word_map = new map< string, loc*, less<string>, allocator >;

typedef map<string,loc*,less<string>,allocator>::value_type value_type;
typedef set<string,less<string>,allocator>::difference_type diff_type;

set<string,less<string>,allocator> exclusion_set;

ifstream infile( "exclusion_set" );
if ( !infile )
{
static string default_excluded_words[25] = {
"the","and","but","that","then","are","been",
"can","can't","cannot","could","did","for",
"had","have","him","his","her","its","into",
"were","which","when","with","would"
};

cerr << "warning! unable to open word exclusion file! -- "
<< "using default set\n";

copy( default_excluded_words, default_excluded_words+25, inserter( exclusion_set, exclusion_set.begin() ));
}
else {
istream_iterator< string, diff_type > input_set( infile ), eos;
copy( input_set, eos, inserter( exclusion_set, exclusion_set.begin() ));
}

// iterate through the the words, entering the key/pair

vector<string,allocator> *text_words = text_locations->first;
vector<location,allocator> *text_locs = text_locations->second;

register int elem_cnt = text_words->size();
for ( int ix = 0; ix < elem_cnt; ++ix )
{
string textword = ( *text_words )[ ix ];

// exclusion strategies
// less than 3 character or in exclusion set
if ( textword.size() < 3 ||
exclusion_set.count( textword ))
continue;

if ( ! word_map->count((*text_words)[ix] ))
{ // not present, add it:
loc *ploc = new vector<location,allocator>;
ploc->push_back( (*text_locs)[ix] );
word_map->insert( value_type( (*text_words)[ix], ploc ));
}
else (*word_map)[(*text_words)[ix]]->push_back( (*text_locs)[ix] );
}
}

因为太长,所以分开了,接着下面的:
...全文
155 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
redwolf310 2005-04-15
  • 打赏
  • 举报
回复
能不能说清楚点,你所说的我不会操作,谢谢
friendzj 2005-04-15
  • 打赏
  • 举报
回复
这么长?看起来好像是对stl的包含出了问了,所以建议检查 对stl的库文件的应用
redwolf310 2005-04-15
  • 打赏
  • 举报
回复
G:\lk\cpp\ch06\textquery\tquery.cpp(241) : see reference to class template instantiation 'std::insert_iterator<std::set<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::less<std::basic_string<char,std::char_traits<ch
ar>,std::allocator<char> > >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > > >' being compiled
G:\lk\cpp\ch06\textquery\tquery.cpp(244) : error C2664: '__thiscall std::istream_iterator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int,struct std::char_traits<int> >::std::istream_iterator<class std::ba
sic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int,struct std::char_traits<int> >(class std::basic_istream<int,struct std::char_traits<int> > &)' : cannot convert parameter 1 from 'class std::basic_ifstream<char,struct st
d::char_traits<char> >' to 'class std::basic_istream<int,struct std::char_traits<int> > &'
A reference that is not to 'const' cannot be bound to a non-lvalue
d:\program files\microsoft visual studio\vc98\include\utility(25) : warning C4786: 'std::_Tree<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const
,std::vector<std::pair<short,short>,std::allocator<std::pair<short,short> > > *>,std::map<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::vector<std::pair<short,short>,std::allocator<std::pair<short,short> > > *,std::less<s
td::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::vector<std::pair<short,short>,std::allocator<std::pair<short,short> > > *> >::_Kfn,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char>
> >,std::allocator<std::vector<std::pair<short,short>,std::allocator<std::pair<short,short> > > *> >::iterator' : identifier was truncated to '255' characters in the debug information
G:\lk\cpp\ch06\textquery\tquery.cpp(268) : see reference to class template instantiation 'std::pair<std::_Tree<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::pair<std::basic_string<char,std::char_traits<char>,std::
allocator<char> > const ,std::vector<std::pair<short,short>,std::allocator<std::pair<short,short> > > *>,std::map<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::vector<std::pair<short,short>,std::allocator<std::pair<short,
short> > > *,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::vector<std::pair<short,short>,std::allocator<std::pair<short,short> > > *> >::_Kfn,std::less<std::basic_string<char,std::char_traits<ch
ar>,std::allocator<char> > >,std::allocator<std::vector<std::pair<short,short>,std::allocator<std::pair<short,short> > > *> >::iterator,bool>' being compiled
Error executing cl.exe.

tquery.obj - 1 error(s), 16 warning(s)
redwolf310 2005-04-15
  • 打赏
  • 举报
回复
现在的警告我看不懂,请高手指点,谢谢
--------------------Configuration: textquery - Win32 Debug--------------------
Compiling...
tquery.cpp
G:\lk\cpp\ch06\textquery\tquery.cpp(26) : warning C4786: 'std::pair<std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > > *,std::vecto
r<std::pair<short,short>,std::allocator<std::pair<short,short> > > *>' : identifier was truncated to '255' characters in the debug information
G:\lk\cpp\ch06\textquery\tquery.cpp(26) : warning C4786: 'std::map<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::vector<std::pair<short,short>,std::allocator<std::pair<short,short> > > *,std::less<std::basic_string<char,s
td::char_traits<char>,std::allocator<char> > >,std::allocator<std::vector<std::pair<short,short>,std::allocator<std::pair<short,short> > > *> >' : identifier was truncated to '255' characters in the debug information
G:\lk\cpp\ch06\textquery\tquery.cpp(114) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char
,std::char_traits<char>,std::allocator<char> > const &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,int>' : identifier was truncated to '255' characters in the debug information
G:\lk\cpp\ch06\textquery\tquery.cpp(114) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,std::
char_traits<char>,std::allocator<char> > &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,int>' : identifier was truncated to '255' characters in the debug information
d:\program files\microsoft visual studio\vc98\include\xtree(120) : warning C4786: 'std::_Tree<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,
std::vector<std::pair<short,short>,std::allocator<std::pair<short,short> > > *>,std::map<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::vector<std::pair<short,short>,std::allocator<std::pair<short,short> > > *,std::less<st
d::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::vector<std::pair<short,short>,std::allocator<std::pair<short,short> > > *> >::_Kfn,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char>
> >,std::allocator<std::vector<std::pair<short,short>,std::allocator<std::pair<short,short> > > *> >' : identifier was truncated to '255' characters in the debug information
d:\program files\microsoft visual studio\vc98\include\map(46) : see reference to class template instantiation 'std::_Tree<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::pair<std::basic_string<char,std::char_traits<
char>,std::allocator<char> > const ,std::vector<std::pair<short,short>,std::allocator<std::pair<short,short> > > *>,std::map<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::vector<std::pair<short,short>,std::allocator<std::
pair<short,short> > > *,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::vector<std::pair<short,short>,std::allocator<std::pair<short,short> > > *> >::_Kfn,std::less<std::basic_string<char,std::cha
r_traits<char>,std::allocator<char> > >,std::allocator<std::vector<std::pair<short,short>,std::allocator<std::pair<short,short> > > *> >' being compiled
G:\lk\cpp\ch06\textquery\tquery.cpp(221) : see reference to class template instantiation 'std::map<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::vector<std::pair<short,short>,std::allocator<std::pair<short,short>
> > *,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::vector<std::pair<short,short>,std::allocator<std::pair<short,short> > > *> >' being compiled
d:\program files\microsoft visual studio\vc98\include\xtree(120) : warning C4786: 'std::_Tree<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,
std::vector<std::pair<short,short>,std::allocator<std::pair<short,short> > > *>,std::map<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::vector<std::pair<short,short>,std::allocator<std::pair<short,short> > > *,std::less<st
d::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::vector<std::pair<short,short>,std::allocator<std::pair<short,short> > > *> >::_Kfn,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char>
> >,std::allocator<std::vector<std::pair<short,short>,std::allocator<std::pair<short,short> > > *> >::const_iterator' : identifier was truncated to '255' characters in the debug information
fengbaotiaorulei 2005-04-15
  • 打赏
  • 举报
回复
#include <algorithm>
#include <string>
#include <vector>
#include <utility>
#include <map>
#include <set>

#include <iostream>
#include <fstream>

#include <stddef.h>
#include <ctype.h>
#include "tquery.h"
using namespace std;

先把这个改成这个

#include <algorithm>
#include <string>
#include <vector>
#include <utility>
#include <map>
#include <set>

#include <iostream>
#include <fstream>

using namespace std;

#include <stddef.h>
#include <ctype.h>
#include "tquery.h"
redwolf310 2005-04-15
  • 打赏
  • 举报
回复
void
TextQuery::
query_text()
{
string query_text;

do {
cout << "enter a word against which to search the text.\n"
<< "to quit, enter a single character ==> ";
cin >> query_text;

if ( query_text.size() < 2 ) break;

string caps( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
string::size_type pos = 0;
while (( pos = query_text.find_first_of( caps, pos )) != string::npos )
query_text[ pos ] = tolower( query_text[pos] );

// if we index into map, query_text is entered, if absent
// not at all what we should wish for ...

if ( !word_map->count( query_text )) {
cout << "\nSorry. There are no entries for "
<< query_text << ".\n\n";
continue;
}

loc *ploc = (*word_map)[ query_text ];

set<short,less<short>,allocator> occurrence_lines;
loc::iterator liter = ploc->begin(), liter_end = ploc->end();

while ( liter != liter_end ) {
occurrence_lines.insert(occurrence_lines.end(), (*liter).first);
++liter;
}

register int size = occurrence_lines.size();
cout << "\n" << query_text
<< " occurs " << size
<< (size == 1 ? " time:" : " times:")
<< "\n\n";

set<short,less<short>,allocator>::iterator it=occurrence_lines.begin();
for ( ; it != occurrence_lines.end(); ++it ) {
int line = *it;

cout << "\t( line "
// don't confound user with text lines starting at 0 ...
<< line + 1 << " ) "
<< (*lines_of_text)[line] << endl;
}

cout << endl;
}
while ( ! query_text.empty() );
cout << "Ok, bye!\n";
}

void
TextQuery::
display_map_text()
{
typedef map<string,loc*,less<string>,allocator> map_text;
map_text::iterator iter = word_map->begin(), iter_end = word_map->end();

while ( iter != iter_end ) {
cout << "word: " << (*iter).first << " (";

int loc_cnt = 0;
loc *text_locs = (*iter).second;
loc::iterator liter = text_locs->begin(),
liter_end = text_locs->end();

while ( liter != liter_end )
{
if ( loc_cnt )
cout << ",";
else ++loc_cnt;

cout << "(" << (*liter).first
<< "," << (*liter).second << ")";

++liter;
}

cout << ")\n";
++iter;
}

cout << endl;
}

void
TextQuery::
display_text_locations()
{
vector<string,allocator> *text_words = text_locations->first;
vector<location,allocator> *text_locs = text_locations->second;

register int elem_cnt = text_words->size();

if ( elem_cnt != text_locs->size() )
{
cerr << "oops! internal error: word and position vectors "
<< "are of unequal size\n"
<< "words: " << elem_cnt << " "
<< "locs: " << text_locs->size()
<< " -- bailing out!\n";
exit( -2 );
}

for ( int ix = 0; ix < elem_cnt; ix++ )
{
cout << "word: " << (*text_words)[ ix ] << "\t"
<< "location: ("
<< (*text_locs)[ix].first << ","
<< (*text_locs)[ix].second << ")"
<< "\n";
}

cout << endl;
}

}

-----------------------tquery.cpp---------------------------------
#include <algorithm>
#include <string>
#include <vector>
#include <utility>
#include <map>
#include <set>

#include <iostream>
#include <fstream>

#include <stddef.h>
#include <ctype.h>
#include "tquery.h"
using namespace std;

int main()
{
lin_kai::TextQuery tq;
tq.doit();
tq.query_text();
tq.display_map_text();
return 0;
}

--------------------Configuration: tquery - Win32 Debug--------------------
Compiling...
tquery.cpp
g:\lk\cpp\ch06\text_query\tquery\tquery.h(4) : error C2143: syntax error : missing ';' before '<'
g:\lk\cpp\ch06\text_query\tquery\tquery.h(4) : error C2059: syntax error : '<'
g:\lk\cpp\ch06\text_query\tquery\tquery.h(5) : error C2143: syntax error : missing ';' before '<'
g:\lk\cpp\ch06\text_query\tquery\tquery.h(5) : error C2059: syntax error : '<'
g:\lk\cpp\ch06\text_query\tquery\tquery.h(6) : warning C4091: 'typedef ' : ignored on left of 'int' when no variable is declared
g:\lk\cpp\ch06\text_query\tquery\tquery.h(6) : error C2143: syntax error : missing ';' before '<'
g:\lk\cpp\ch06\text_query\tquery\tquery.h(6) : error C2059: syntax error : '<'
g:\lk\cpp\ch06\text_query\tquery\tquery.h(7) : warning C4091: 'typedef ' : ignored on left of 'int' when no variable is declared
g:\lk\cpp\ch06\text_query\tquery\tquery.h(7) : error C2143: syntax error : missing ';' before '<'
g:\lk\cpp\ch06\text_query\tquery\tquery.h(7) : error C2059: syntax error : '<'
g:\lk\cpp\ch06\text_query\tquery\tquery.h(13) : error C2061: syntax error : identifier 'string'
g:\lk\cpp\ch06\text_query\tquery\tquery.h(33) : error C2061: syntax error : identifier 'string'
g:\lk\cpp\ch06\text_query\tquery\tquery.h(37) : error C2059: syntax error : '<'
g:\lk\cpp\ch06\text_query\tquery\tquery.h(37) : error C2238: unexpected token(s) preceding ';'
g:\lk\cpp\ch06\text_query\tquery\tquery.h(38) : error C2143: syntax error : missing ';' before '*'
g:\lk\cpp\ch06\text_query\tquery\tquery.h(38) : error C2501: 'text_loc' : missing storage-class or type specifiers
g:\lk\cpp\ch06\text_query\tquery\tquery.h(38) : error C2501: 'text_locations' : missing storage-class or type specifiers
g:\lk\cpp\ch06\text_query\tquery\tquery.h(39) : error C2143: syntax error : missing ';' before '<'
g:\lk\cpp\ch06\text_query\tquery\tquery.h(39) : error C2501: 'map' : missing storage-class or type specifiers
g:\lk\cpp\ch06\text_query\tquery\tquery.h(39) : error C2059: syntax error : '<'
g:\lk\cpp\ch06\text_query\tquery\tquery.h(39) : error C2238: unexpected token(s) preceding ';'
g:\lk\cpp\ch06\text_query\tquery\tquery.h(40) : error C2146: syntax error : missing ';' before identifier 'filt_elems'
g:\lk\cpp\ch06\text_query\tquery\tquery.h(40) : error C2501: 'filt_elems' : missing storage-class or type specifiers
g:\lk\cpp\ch06\text_query\tquery\tquery.h(43) : error C2039: 'filt_elems' : is not a member of 'TextQuery'
g:\lk\cpp\ch06\text_query\tquery\tquery.h(9) : see declaration of 'TextQuery'
g:\lk\cpp\ch06\text_query\tquery\tquery.h(43) : error C2146: syntax error : missing ';' before identifier 'filt_elems'
g:\lk\cpp\ch06\text_query\tquery\tquery.h(43) : error C2501: 'string' : missing storage-class or type specifiers
g:\lk\cpp\ch06\text_query\tquery\tquery.h(43) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

tquery.obj - 25 error(s), 2 warning(s)

小弟刚开始学习c++对VC环境还不熟悉,请高手指点一下!

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧