Hi all,
I've been programming in C++ for a couple of days now. I'm having problems with a Keyword in Context program. The program is meant to display user input:
"The quick brown fox"
and display the following output:
" __________________The quick brown fox
____________The___quick brown fox
_______The quick___brown fox
The quick brown____fox"
Currently getting two error messages I can't wrap my head around. I'll display the code first, and the error messages after.
Main.cpp:
This code compiled fine - not sure if there are any more errors in it.
Index.h:
Also seems to be fine. Currently having trouble with index.cpp below.
Index.cpp:
The first error message I'm getting is on this line in index.cpp:
The error message is below.
Is this just because I'm using an index position and 0 rather than iterators? Does this function only take iterators? I can't quite work this error message out, but I can start to guess where the problem is. It says no matching function - so either I've left out the reference somewhere and it doesn't know what erase means, or I'm using the function incorrectly and it will only accept iterators as the range.
This next error message, threw me a bit more... I commented out the above code, fixed a couple of forgotten ";" errors and tried to compile again. There are no remaining error messages in the files themselves, instead I get directed to some new files that are part of the C++ implimentation: stl_iterator_base_types.h and stl_algobase.h
The error message is:
I'm ignoring the warnings from my program for now - I'll deal with them if I need to after I've sorted out the main errors. Obviously there is nothing wrong with the standard library iterator header, or algorithm header - usually the iterator head error pops up, the last time I tried the stl_algobase.h error popped up. I don't remember changing anything.
In the stl_iterator_base_types.h file, the error message is on this line:
...and in algobase:
I suspect it has something to do with this section in my main.cpp:
I'm all for trying to debug my own programs and learn, but what the hell does that mean. At my stage, it's the equivalent of shouting "You've done something wrong, but I'm not telling you what." :lol:
I've been programming in C++ for a couple of days now. I'm having problems with a Keyword in Context program. The program is meant to display user input:
"The quick brown fox"
and display the following output:
" __________________The quick brown fox
____________The___quick brown fox
_______The quick___brown fox
The quick brown____fox"
Currently getting two error messages I can't wrap my head around. I'll display the code first, and the error messages after.
Main.cpp:
C++:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <cctype>
#include <iostream>
#include "index.h"
using namespace std;
// Structure - Rot_String objects.
// Find the max width from a vector of strings.
string::size_type width(const vector<string>& v)
{
string::size_type maxlen = 0;
for(vector<string>::size_type i = 0; i != v.size(); ++i)
maxlen = max(maxlen, v[i].size());
return maxlen;
}
int main()
{
//User input
cout << "Please enter a short sentence." << endl;
string s;
getline(cin, s);
// Rotate function called. Return stored in rotated.
vector<Rot_String> rotated = rotate(s);
vector<string> left;
vector<string> right;
vector<string> print; //Final string to be printed.
string::size_type len = rotated.size(); //Find length of rotated.
// Form Left and Right vectors.
// We have processed i lines.
for (string::size_type i = 0; i != len; ++i)
{
string::size_type split = rotated[i].split;
string rot = rotated[i].str;
string::size_type end = rot.size();
right.push_back(rot.substr(0, split));
left.push_back(rot.substr(split, end));
}
// Find max characters in Left
string::size_type maxlen = width(left);
// Create final vector.
for (vector<string>::size_type i = 0; i != left.size(); ++ i)
{
print.push_back(string(maxlen - left[i].size(), ' ') + "\t" + right[i]);
}
// Print final vector
ostream_iterator<string>ofile(cout, "\n");
copy(print.begin(), print.end(), ofile);
cout << endl;
return 0;
}
This code compiled fine - not sure if there are any more errors in it.
Index.h:
C++:
#ifndef GUARD_pics_h
#define GUARD_pics_h
#include <string>
#include <vector>
using std::string;
using std::vector;
struct Rot_String {
string str; // Stores rotated sentence.
string::size_type split; // Stores split character point.
};
bool compare(const Rot_String& x, const Rot_String& y);
std::vector<string> seperate(const string& s);
std::vector<Rot_String> rotate(const string& s);
#endif
Also seems to be fine. Currently having trouble with index.cpp below.
Index.cpp:
C++:
#include <algorithm>
#include <iterator>
#include <string>
#include <vector>
#include <cctype>
#include <iostream>
#include "index.h"
using namespace std;
// File contains functions for the index.
bool compare(const Rot_String& x, const Rot_String& y)
{
return x.str < y.str;
}
// seperate function. Splits string into seperated words stored in a vector.
vector<string> seperate(const string& s)
{
vector<string> ret;
typedef string::size_type string_size;
string_size i = 0;
// invariant: we have processed characters [original value of i, i)
while (i != s.size()){
// ignore leading blanks
// invariant: characters in range [original i, current i) are all spaces
while (i != s.size() && isspace(s[i]))
++i;
//find end of next word
string_size j = i;
// invariant: none of the characters in range [original j, current j) is a space
while (j != s.size() && !isspace(s[j]))
++j;
// if we found some nonwhitespace characters
if (i != j){
// copy from s starting at i and taking j-i chars
ret.push_back(s.substr(i, j - i));
i = j;
}
}
return ret;
}
// Rotate function. Returns a vector of Rot_String types.
vector<Rot_String> rotate(const string& s)
{
vector<Rot_String> ret;
vector<string> split = seperate(s);
// we have rotated i lines so far
for (int i = 0; i != split.size(); ++i)
{
// Create a Rot_String object
Rot_String rote;
// String is split into a vector
vector<string> temp;
vector<string> splitcopy = split;
// Words 0 - i are removed and stored in a temp vector
copy(splitcopy[0], splitcopy[i], temp[0]);
splitcopy.erase(0, i);
// Remaining words reformed into string.
string::size_type len = splitcopy.size();
for (int x = 0; x != len; ++x)
{
rote.str += splitcopy[x] + " ";
}
rote.split = rote.str.size(); // Split character is counted and added to object.
// Removed words are added to end of string.
len = temp.size();
for (int y = 0; y != len; ++y){
rote.str += temp[y] + " ";
}
// Rot_String object is added to vector ret.
ret.push_back(rote);
}
//sort ret alphabetically
sort(ret.begin(), ret.end(), compare);
return ret; //return sorted vector.
}
The first error message I'm getting is on this line in index.cpp:
splitcopy.erase(0, i);
The error message is below.
Code:
C:\Users\Will\Documents\Documents - Main\TSF\C++\Permuted Index\index.cpp|69|error: no matching function for call to '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> > > >::erase(int, int&)'|
Is this just because I'm using an index position and 0 rather than iterators? Does this function only take iterators? I can't quite work this error message out, but I can start to guess where the problem is. It says no matching function - so either I've left out the reference somewhere and it doesn't know what erase means, or I'm using the function incorrectly and it will only accept iterators as the range.
This next error message, threw me a bit more... I commented out the above code, fixed a couple of forgotten ";" errors and tried to compile again. There are no remaining error messages in the files themselves, instead I get directed to some new files that are part of the C++ implimentation: stl_iterator_base_types.h and stl_algobase.h
The error message is:
Code:
C:\Users\Will\Documents\Documents - Main\TSF\C++\Permuted Index\index.cpp||In function 'std::vector<Rot_String, std::allocator<Rot_String> > rotate(const std::string&)':|
C:\Users\Will\Documents\Documents - Main\TSF\C++\Permuted Index\index.cpp|58|warning: comparison between signed and unsigned integer expressions|
C:\Users\Will\Documents\Documents - Main\TSF\C++\Permuted Index\index.cpp|73|warning: comparison between signed and unsigned integer expressions|
C:\Users\Will\Documents\Documents - Main\TSF\C++\Permuted Index\index.cpp|81|warning: comparison between signed and unsigned integer expressions|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\stl_algobase.h|388|instantiated from '_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false, _II = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _OI = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\stl_algobase.h|436|instantiated from '_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false, _II = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _OI = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\stl_algobase.h|468|instantiated from '_OI std::copy(_II, _II, _OI) [with _II = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _OI = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]'|
C:\Users\Will\Documents\Documents - Main\TSF\C++\Permuted Index\index.cpp|68|instantiated from here|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\stl_iterator_base_types.h|127|error: no type named 'iterator_category' in 'struct std::basic_string<char, std::char_traits<char>, std::allocator<char> >'|
||=== Build finished: 1 errors, 3 warnings ===|
I'm ignoring the warnings from my program for now - I'll deal with them if I need to after I've sorted out the main errors. Obviously there is nothing wrong with the standard library iterator header, or algorithm header - usually the iterator head error pops up, the last time I tried the stl_algobase.h error popped up. I don't remember changing anything.
In the stl_iterator_base_types.h file, the error message is on this line:
127 typedef typename _Iterator::iterator_category iterator_category;
...and in algobase:
388 typedef typename iterator_traits<_II>::value_type _ValueTypeI;
I suspect it has something to do with this section in my main.cpp:
C++:
ostream_iterator<string>ofile(cout, "\n");
copy(print.begin(), print.end(), ofile);
cout << endl;
I'm all for trying to debug my own programs and learn, but what the hell does that mean. At my stage, it's the equivalent of shouting "You've done something wrong, but I'm not telling you what." :lol:
Last edited by a moderator: