0

This is part of my first ever C++ program, and I'm very unsure of the syntax I've used. Looking for fixes and suggestions to better the code.

This is my struct, containing 3 maps:

typedef struct {    
std::map<std::string, int> firstCounts;    
std::map<std::string, int> pairCounts;    
std::map<std::string, std::list<std::string>> follows;     
} LanguageModel;

These are the functions that need to work with this LanguageModel struct:

using namespace std;    

string buildPair(string s1, string s2) {    
    string s3;    
    s3 = s1 + " " + s2;    
    return s3;    
}

LanguageModel countSequence(LanguageModel &model, vector<string> &Seq) {    
    //append "<start>" and "<end>" to the vector of strings    
    vector<string>::iterator v = Seq.begin();    
    Seq.push_front("<START>");    
    v = Seq.end();    
    Seq.push_end("<END">);    
    v = Seq.begin();     

    //create word pairs    
    vector<string> pairs;     
    for (int n = 0; n <= Seq.size(); n++) {    
    pairs.at(n) = buildPair(Seq[n], Seq[n+1]);    
    }    

    //feed each word to map 1 with number of times it was seen    
    for (int m = 0; m <= Seq.size(); m++) {    
    ++model.firstCounts[Seq[m]];    
    model.firstCounts[Seq[m]] =  count(Seq.begin(), Seq.end(), Seq[m]);    
    }


    //feed each word pair and the number of times it was seen    
    for (int k = 0; k <= Seq.size(); k++) {    
    ++model.pairCounts[Seq[k]];    
    model.pairCounts[Seq[k]] =  count(Seq.begin(), Seq.end(), Seq[m]);    
    }

    //feed each unique first word in a word pair and the second words in  the pairs
    for (int l = 0; l <= Seq.size(); l++) {    
    istringstream iss(pairs[l]);    
     string sub;    
        iss >> sub;    
     string sub2;    
        iss >> sub2;   

        if (Seq[l] = sub) {    
            ++model.follows[sub];    
            model.follows[sub].push_back(sub2);     
        }        

    }

return model;
}


string genNext(LanguageModel &model2, string &testWord, int Number) {

    //use results of countSequence 

    return model2.follows[testWord][Number];

}

The code doesn't compile yet, of course.

These are the errors I am getting:

LangModel.h:28:1: error: ‘string’ does not name a type
 string buildPair(std::string s1, std::string s2);
 ^
LangModel.h:30:63: error: ‘string’ was not declared in this scope
 LanguageModel countSequence(LanguageModel &model, std::vector<string> &Seq);
                                                               ^
LangModel.h:30:63: note: suggested alternative:
In file included from /usr/include/c++/4.8/iosfwd:39:0,
                 from /usr/include/c++/4.8/ios:38,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from LangModel.h:13,
                 from hw5.cpp:10:
/usr/include/c++/4.8/bits/stringfwd.h:62:33: note:   ‘std::string’
   typedef basic_string<char>    string;   
                                 ^
In file included from hw5.cpp:10:0:
LangModel.h:30:69: error: template argument 1 is invalid
 LanguageModel countSequence(LanguageModel &model, std::vector<string> &Seq);
                                                                     ^
LangModel.h:30:69: error: template argument 2 is invalid
LangModel.h:32:1: error: ‘string’ does not name a type
 string genNext(LanguageModel &model2, std::string &testWord, int Number);
 ^
hw5.cpp: In function ‘int main()’:
hw5.cpp:34:1: error: expected ‘}’ at end of input

Update: Fixed the errors above. Now getting these:

LangModel.cpp: In function ‘LanguageModel countSequence(LanguageModel&, std::vector<std::basic_string<char> >&)’:
LangModel.cpp:37:6: error: ‘class std::vector<std::basic_string<char> >’ has no member named ‘push_front’
  Seq.push_front("<START>");
      ^
LangModel.cpp:39:6: error: ‘class std::vector<std::basic_string<char> >’ has no member named ‘push_end’
  Seq.push_end("<END">);
      ^
LangModel.cpp:39:22: error: expected primary-expression before ‘)’ token
  Seq.push_end("<END">);
                      ^
LangModel.cpp:44:32: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int n = 0; n <= Seq.size(); n++) {
                                ^
LangModel.cpp:49:32: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int m = 0; m <= Seq.size(); m++) {
                                ^
LangModel.cpp:55:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int k = 0; k <= Seq.size(); k++) {
                                   ^
LangModel.cpp:57:67: error: ‘m’ was not declared in this scope
     model.pairCounts[Seq[k]] =  count(Seq.begin(), Seq.end(), Seq[m]);
                                                                   ^
LangModel.cpp:61:32: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int l = 0; l <= Seq.size(); l++) {
                                ^
LangModel.cpp:62:20: error: variable ‘std::istringstream iss’ has initializer but incomplete type
  istringstream iss(pairs[l]);
                    ^
LangModel.cpp:68:25: error: could not convert ‘(&(& Seq)->std::vector<_Tp, _Alloc>::operator[]<std::basic_string<char>, std::allocator<std::basic_string<char> > >(((std::vector<std::basic_string<char> >::size_type)l)))->std::basic_string<_CharT, _Traits, _Alloc>::operator=<char, std::char_traits<char>, std::allocator<char> >((*(const std::basic_string<char>*)(& sub)))’ from ‘std::basic_string<char>’ to ‘bool’
         if (Seq[l] = sub) {
                         ^
LangModel.cpp:69:10: error: no match for ‘operator++’ (operand type is ‘std::map<std::basic_string<char>, std::list<std::basic_string<char> > >::mapped_type {aka std::list<std::basic_string<char> >}’)
          ++model.follows[sub];
          ^
LangModel.cpp: In function ‘std::string genNext(LanguageModel&, std::string&, int)’:
LangModel.cpp:80:33: error: no match for ‘operator[]’ (operand types are ‘std::map<std::basic_string<char>, std::list<std::basic_string<char> > >::mapped_type {aka std::list<std::basic_string<char> >}’ and ‘int’)
  return model2.follows[testWord][Number];
                                 ^
LangModel.cpp:81:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^

Fixed most of the errors above. These are the ones left. I'm sure these are all syntax related, but not sure how to fix them.

LangModel.cpp:69:25: error: could not convert ‘(&(& Seq)->std::vector<_Tp, _Alloc>::operator[]<std::basic_string<char>, std::allocator<std::basic_string<char> > >(((std::vector<std::basic_string<char> >::size_type)l)))->std::basic_string<_CharT, _Traits, _Alloc>::operator=<char, std::char_traits<char>, std::allocator<char> >((*(const std::basic_string<char>*)(& sub)))’ from ‘std::basic_string<char>’ to ‘bool’
         if (Seq[l] = sub) {
                         ^
LangModel.cpp:70:10: error: no match for ‘operator++’ (operand type is ‘std::map<std::basic_string<char>, std::list<std::basic_string<char> > >::mapped_type {aka std::list<std::basic_string<char> >}’)
          ++model.follows[sub];
          ^
LangModel.cpp: In function ‘std::string genNext(LanguageModel&, std::string&, int)’:
LangModel.cpp:81:33: error: no match for ‘operator[]’ (operand types are ‘std::map<std::basic_string<char>, std::list<std::basic_string<char> > >::mapped_type {aka std::list<std::basic_string<char> >}’ and ‘int’)
  return model2.follows[testWord][Number];
NewToThis
  • 141
  • 1
  • 3
  • 9

2 Answers2

1

The first error you are getting is:

LangModel.h:28:1: error: ‘string’ does not name a type
 string buildPair(std::string s1, std::string s2);
 ^

This means that string is not known to the compiler. You need to, at the top of your program, add:

#include <string>

I see you also have using namespace std;. While this works for small programs, see Why is “using namespace std;” considered bad practice?. It would be better to remove the using namespace std; and use std::string instead of just string.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • Thanks, Greg! #include was already included at the top. I did fix the previous errors, by adding std:: in front of every instance of string. – NewToThis Apr 02 '15 at 02:22
  • There are however more errors now, all from LangModel.cpp - which is the source file with all my functions. – NewToThis Apr 02 '15 at 02:24
  • I'm not sure what reference material you are using, because `std::vector` has neither `push_front` nor `push_end`. http://cppreference.com is an excellent library reference site. Your other errors are pretty straightforward, you'll need an `#include ` too. – Greg Hewgill Apr 02 '15 at 02:28
  • So what is the alternative for push_front? – NewToThis Apr 02 '15 at 02:34
0

1) You probably want a double-equals here

if (Seq[l] = sub)

should look like

if (Seq[l] == sub)

2) "follows" maps a string to a list, and the compiler is telling you that you cannot increment a list.

++model.follows[sub];
Jon
  • 26
  • 2