C++ Sort Error "No instance of overloaded function.." - c++

I'm still very new to C++ and programming in general, so I apologize if haven't the right information the first time
I started learning how to code with the book "Programming: Principles and Practice Using C++ (2nd Edition)" by Bjarne Stroustrup and I ran into some errors while using the code provided in chapter 4.6.4. Every time I go to run the code it tells me about "std::sort" and that there's no instance of overloaded function "std::sort" matches the argument list. There's also a new error in line 16 with i-1 as the IDE (Visual Studio 2013 Express) says the identifier is undefined.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
int main()
{
std::vector<std::string>words;
for (std::string temp; std::cin >> temp;)
words.push_back(temp);
std::cout << "Number of words: " << words.size() << std::endl;
std::sort(words);
for (int i = 0; i<words.size(); ++i)
if (i == 0 || words[i–1] != words[i]) // is this a new word?
std::cout << words[i] << "\n";
}
I can't seem to find out what's causing the error as I've put the required #include but it still shows the error. Any explanation would help tremendously.

std::sort takes a pair of iterators.
std::sort(words.begin(), words.end());
You could define your own helper function that takes one argument.
template<typename Container>
inline void sort(Container& c)
{
std::sort(std::begin(c), std::end(c));
}
You probably want to create your own namespace for the helper function.

Related

implicit instantiation of undefined template error

I'm using Programming: Principles and Practice Using C++ (2nd Edition) for study and having some problem.
Trying out the code written below gives me a implicit instantiation of undefined template error.
I tried addding #include<vector> but adding the code gives me a new compile error of no matching function for call to sort().
With my poor knowledge, I can't find the way to solve this problem so it will be really thankful to give me an advice solving it.
#include <iostream>
using namespace std;
inline void keep_window_open(){char ch; cin>> ch;}
int main(){
vector<string>words;
for(string temp; cin >>temp;)
words.push_back(temp);
cout << "Number of words:" << words.size() <<'\n';
sort(words);
for(int i = 0; i<words.size(); ++i)
if(i == 0 || words[i-1]! =words[i])
cout << words[i] << "\n";
}
You have two core problems. The first one is you're missing several headers:
#include <string>
#include <vector>
#include <algorithm>
string is for, well, std::string, and similarly, vector for std::vector. algorithm is for std::sort.
The second one is that's not the how std::sort should be called, since it needs a pair of iterators:
sort(words.begin(), words.end());

GCC template : error : expected ',' or '...' before '< ' token

I'm a new bee to C++.
I'm having below errors when tying to compile below templates example copied from "Essential C++", I simply copied the example on the book.
"error : expected ',' or '...' before '< ' token"
"error : 'vec' was not declared in this scope
I was wondering what could be the problem, could you help spare some time to give a hint? so much appreciate!
'''
#include <iostream>
template <typename elemType>
void display_message(const string &msg, const vector<elemType> &vec)
{
cout << msg;
for(int ix = 0; ix < vec.size(); ++ix)
{
elemType elem = vec[ix];
cout << elem << ' ';
}
}
int main()
{
int size = 10;
ocnst vector<int> ivec1 = fibon_seq(5);
if (is_size_ok(size))
{
display_message(msg, size);
}
display_message(msg, ivec1);
cout << "Hello world!" << endl;
return 0;
}
'''
There's a lot of issues with the code that prevent from compilation:
The functions fibon_seq and is_size_ok are not defined. You have to define them in this file or include them with a #define macro.
The code appears to be including the std namespace. You need to include this with using namespace std;, usually at the beginning of the code, after library includes.
The variable msg is passed as an argument in a function, but it's not defined.
This function call display_message(msg, size); is being passed the wrong argument. size is an integer, but the function expects vector<int>.
5.ocnst is a typo. It should be const.
There are a few problems with your code:
ocnst should be const.
Missing #include <string>
Missing #include <vector>
The last one is the likely cause of that particular error.
Thank you so much for your time and generous help!H
How embarrassed, I did forgot to add
#include since I copied the code from previous code.
"essential C++" seems a good book that recommend by many ppl. I'm still trying to familalize the language.
if you think there is better material, please give me a piece of advise, Thanks!

Problems with small C++ bitset code

I have tried to write the following code into my compiler and compile it:
#include <iostream>
#include <bitset>
using namespace std;
void binary(int a)
{
cout << bitset<8>(a).to_string() << endl;
}
int main()
{
binary(16);
system("pause");
return 0;
}
It should give me a binary output but I keep getting an error:
In function `void binary(int)':
no matching function for call to `std::bitset<8u>::to_string()'
I am new to C++ and dont really know what this means, please help me.
I think older versions of bitset::to_string<T>() takes a template argument. So this should work:
cout << bitset<8>(a).to_string<char>() << endl;
bitset don't have a to_string method (stl does not use to_string anyway). You should iterate on values yourself.

Works with g++ but not MSVC++ [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why isn’t cin >> string working with Visual C++ 2010?
The first program I wrote refuses to compile in Visual C++, and it looks like it's complaining that the '>>' operator isn't defined for istream.
After looking it over carefully, it seemed to be correct, so I checked with g++ and it compiles fine (and has no warnings with -Wall).
So why does it work with g++ but not Visual C++?
Here is the program:
#include <iostream>
#include <list>
int main() {
std::list<std::string> list;
std::string str = "";
std::cin >> str;
while (str.compare("q") != 0) {
list.push_back(str);
std::cin >> str;
}
std::cout << "You entered: \n";
for (std::list<std::string>::iterator i = list.begin(); i != list.end(); i++) {
std::cout << *i << std::endl;
}
return 0;
}
I had thought C++ code written for Visual C++ and C++ code written for g++ would be nearly identical in most circumstances.
How different are they, how often would you say these kinds of issues come up, and do you know of anywhere I can find some of these differences/gotchas?
Different compilers have different headers that internally include other headers. gcc is probably including <string> inside of <iostream>, while Visual Studio's <iostream> doesn't include <string>. Try putting:
#include <string>
At the top with your other includes. <string> is the header file that defines operator>>(std::istream, std::string) (in other words, <string> is the header that "officially" provides the function you need to do std::cin >> str;).

C++11 / g++ : std:: qualifier required in lambda, although "using namespace std" is given

I was trying to discover some of the goodies of the new C++11 standard (using g++ 4.6.2). Playing around with lambdas in a an "all_of" algorithm function, I encountered a strange problem with the std:: qualifier.
I am "using" the std namespace as shown at the beginning of the code snippet. This makes the declaration of the pair variable in the for loop well-defined.
However, I tried the same in the lambda argument used in the "all_of" algorithm. I came across several hard-to-understand error messages, before I realized that a full std:: qualified std::pair would work there, but only pair not.
Am I missing an important point? The declaration of the lambda happens in this file, so the namespace should still be active here, right? Or does the required std:: qualifier depend on some STL code in a different file? Or is it likely to be a bug in g++?
Best regards,
Peter
PS: the code compiles without warnings as pasted here, but removing the std:: in the all_of lambda, I get an error message.
#include <iostream>
#include <memory>
#include <map>
#include <string>
#include <algorithm>
#include <utility>
using namespace std;
void duckburg() {
const int threshold = 100;
map <string, int> money;
money["donald"] = 200;
money["daisy"] = 400;
money["scrooge"] = 2000000;
// obviously, an "auto" type would work here nicely,
// but this way my problem is illustrated more clearly:
for (const pair <string, int> &pair : money) {
cout << pair.first << "\t" << pair.second << endl;
}
if (all_of(money.begin(), money.end(),
[&](std::pair<string, int> p) {
return bool(p.second > threshold);
}))
{
cout << "yes, everyone is rich!";
} else {
cout << "no, some are poor!";
};
}
Edit: Just noticed I received a downvote for this old question. No problem with that, but please elaborate on the reasons. It will help me improve future questions, and in the end the entire community will profit. Thanks!
Rename the variable pair in your for loop.
It's scope should only extend to the end of the for loop and therefore not interfere with your
lambda, but g++ has some code for ancient for-scoping rules where that was not the case, so it can emit better error messages for ancient C++ code.
It looks as if there is a bug in that compatibility code.