implicit instantiation of undefined template error - c++

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());

Related

Does anyone know what header file is missing?

This code works on Code Blocks but not on Visual Studio:
// A simple program that prints string test1:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<string> test1 = { "pooping","reading" };
for (vector<string>::iterator iter = test1.begin(); iter != test1.end(); iter++)
{
cout << *iter << endl;
}
system("pause");
}
The error that Visual Studio Outputs:
Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'std::_Vector_iterator<std::_Vector_val<std::_Simple_types<std::basic_string<char,std::char_traits<char>,std::allocator<char>>>>>' (or there is no acceptable conversion)
std::basic_string is officially defined in the <string> header. See cppreference.
#include<string>
The class template basic_string stores and manipulates sequences of char-like objects. The class is dependent neither on the character type nor on the nature of operations on that type. The definitions of the operations are supplied via the Traits template parameter - a specialization of std::char_traits or a compatible traits class. For more details please see
std::basic_string
after adding header , program won't compile and it will throw error
"error: in C++98 'test1' must be initialized by constructor, not by '{...}'"
check the below program followed another method to initialize vector.
As suggested by other you need to add
#include<string>
// A simple program that prints string test1:
#include <iostream>
#include <vector>
#include<string>
using namespace std;
int main()
{
static const string arr[] = {"pooping","reading"};
vector<string> test1 (arr, arr + sizeof(arr) / sizeof(arr[0]) );
for (vector<string>::iterator iter = test1.begin(); iter != test1.end(); iter++)
{
cout << *iter << endl;
}
system("pause");
}

compiler isnt identifying getline() . moreover it isn't letting cin and cout use strings as well

I have been programming since 3 years in c++. I have used compilers like turbo c++, Dev c++, linux & codeblocks.
Recently, I started using Visual Studio 2014 C++ and I'm facing a problem with strings.
using namespace std;
int main()
{
string s;
cout << "enter string: ";
getline(cin, s);
cout << s;
return 0;
}
However, the compiler isn't identifying getline. Moreover, it isn't letting cin and cout use strings as well. The code seems to work with other compilers (e.g. Turbo C++, Dev C++, Linux, CodeBlocks), but it doesn't compile on Visual Studio.
I'm totally confused what could be the problem here.
You are missing #include <iostream>. Without this, you cannot use std::cout or std::cin. Also, you need #include <string> to use std::getline() and std::string.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
cout<<"enter string: ";
getline(cin,s);
cout<<s;
return 0;
}
You can also put system("pause"); before the return statement to have it pause when it prints the output, in case you wanted to see it.

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

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.

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.

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.