Problems with small C++ bitset code - c++

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.

Related

how do i fix this no matching function for call to 'stoi(int&)'|

i keep getting this error. i know this is a c++ 11 function but it still isnt working with code blocks c++ compiler. am i using this function correctly of is it a problem with the codeblocks compiler. i tried changing the compiler. using the "have g++ follow the c++11 iso standard" i still keep getting this error. or getting the "stoi() does not exist in the current scope" error
#include <iostream>
#include <string>
using namespace std;
int main()
{
int test = 34;
cout << stoi(test);
}
stoi means "String To Int". It will read an int from a std::string (or std::wstring). See also the reference.
You were probably looking for the reverse std::to_string (reference). But you don't need either, there is no need to convert to string before printing:
#include <iostream>
int main()
{
int test = 34;
std::cout << test;
}
stoi means string to int. So it takes a string as an input.
This should work:
string test = "34"; cout << stoi(test);

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

Trying to understand the keyword friend correctly via OOP in c++, coding issues

OK so here's my question, I'm trying to understand the use of friend in C++ with a working example on my PC for reference. I have everything set up in different classes, which are connected to one another with the .h files etc. (I think anyways). Can someone show me where I'm going wrong please? Cause i keep getting compiler errors and I don't understand what it means.
Yes I've researched the C:xxxx errors online, but I can't link the problem to the code I've got... It's obviously wrong! just need a push in the right direction / better understanding of C++... thank you!
//First class
#include <iostream>
#include <string>
using namespace std;
class Tutorial48
{
private:
int var;
int secret = 5;
public:
Tutorial48(int v);
friend class Tutorial48UseFriend;
void PrintVar();
};
// First class .cpp
#include "Tutorial48.h"
Tutorial48::Tutorial48(int v)
{
var = v;
}
void Tutorial48::PrintVar()
{
cout << var << endl;
cout << "Scret variable = " << secret << endl;
}
// Second class, whole point is to demo the friend keyword in C++!
#include "Tutorial48.h"
#include <iostream>
#include <string>
using namespace std;
class Tutorial48UseFriend
{
public:
Tutorial48UseFriend();
void showSecret(Tutorial48 tut48F)
{
// Just trying to increment by 1 so i know it's worked correctly.
tut48F.secret++;
cout << "My new private variabe = " << tut48F.secret << endl;
};
};
// Main function for outputting the whole thing.
#include "Tutorial48.h"
#include "Tutorial48UseFriend.h"
#include <string>
#include <iostream>
int main
{
Tutorial48UseFriend fr;
Tutorial48 t(24);
fr.showSecret(t);
getchar();
return 0;
}
Errors being produced....
Errors
Yeah so that's everything... As i said i'm new to this, so trying to understand it. Thank you to anyone for help in advance, cheers guys.
P.s. I do kind of understand the concepts of friend in c++ how it's used to access private variables of other classes etc. but i have no idea how to code it properly...
You should define the constructor of Tutorial48UseFriend like:
Tutorial48UseFriend(){}
and also pass Tutorial48 object by reference (by using '&'), and instead of making the function void make it return an int and then print it later, so:
int showSecret(Tutorial48& tut48F)
{
return ++tut48F.secret;
}

How do I assign text to the "void PrintIntro" function?

So far I have typed this,
#include iostream
using namespace std;
void PrintIntro();
I want to assign a actual text now to the PrintIntro function so that in my main program I can just type
PrintIntro() and when the program runs the text assign to the function will show.
So far I have tried this after "void PrintIntro();"
{ /*PrintIntro*/
cout <<
"==================================================" << endl;
cout <<
"Welcome to the Math Practice Program!!!!!" << endl;
cout <<
"This Program will help you practice elementary math" << endl;
cout <<
"==================================================" << endl;
/*PrintIntro*/
}
But then I get the error under the "{" symbol indicating that it is "expecting a declaration." I have been searching through notes and messing with this all day and I cannot figure it out. Any help would be appreciated. I am using MS Visual studio Express 2013.
Adding the semicolon after void PrintIntro() tells the compiler that there is a function called PrintIntro that takes no arguments and returns void, and that you are defining it later. This is called a forward declaration. Chances are this is what's happening:
void PrintIntro();
//Compiler: okay, that was a forward declaration
{
//Compiler: wth is this stuff?
}
You want this to happen:
void PrintIntro();
//Compiler: okay, that was a forward declaration
void PrintIntro()
{
//Compiler: oh, this is the definition for that function you told me about earlier
}
Or you want to do it without the forward declaration:
void PrintIntro() //no ';'
{
//Compiler: declaration and function body all in one part - simple!
}
You should also change #include iostream to #include <iostream>
Remove the ; after you declare you function like:
void PrintIntro()
When declaring a function, you do not need to end the function declaration line with a semicolon.
So your function should look like
void PrintIntro(){
blahblahblah...
}
You need to remove the semi column at the end of the declaration
void PrintIntro();
Must be like this
void PrintIntro(){
}
A couple of errors, most of them in the syntax I think. Note that the line #include iostream should actually be #include <iostream> and the ; is missing after void PrintIntro().
Like so
#include <iostream>
using namespace std;
void PrintIntro(){
cout << "Hello world" << endl;
}

Is equal() included by default in the global namespace?

This is a question regarding the default global namespace in C++. I have the following code that compiles and runs properly using g++ clang-500.2.79.
#include <string>
#include <iostream>
using std::string;
using std::endl;
using std::cout;
bool is_palindrome(const string& str){
return equal(str.begin(), str.end(), str.rbegin());
}
int main(){
cout << "Hello is a palindrome: " << is_palindrome("Hello") << endl;
cout << "madam is a palindrome: " << is_palindrome("madam") << endl;
return 0;
}
My questions is, why does this code compile properly? I forgot to put #include <algorithm> and using std::equal at the beginning of my file. So the expected behaviour is for the compiler to complain.
The example at http://en.cppreference.com/w/cpp/algorithm/equal confirms that I should be using std::equal.
To investigate this further, I tried to track down exactly which version of the equal() function was being called. Being a relative newbie to C++ I don't know exactly how to do this either. I tried,
cout << "The function is: " << equal << endl;
Which generated a compiler error with some interesting information:
/usr/include/c++/4.2.1/bits/stl_algobase.h:771:5:
note: 'std::equal' declared here
Try as I might, I can't find information about stl_algobase (or more probably, I most likely don't understand what I've found). Is stl_algobase a set of functions that are automatically included in the global namespace?
A further questions is: What is the proper way to track (code or otherwise) down which function is being called when you are dealing with potentially overloaded or template functions in C++?
equal is in the std namespace. What you are seeing is argument dependent lookup (ADL). Because the arguments are in the std, the name lookup for equal considers that namespace too.
Here's a simplified example:
namespace foo
{
struct Bar {};
}
namespace foo
{
void bar(const Bar&) {}
void bar(int) {}
}
int main()
{
foo::Bar b;
foo::bar(b); // OK
bar(b); // ADL, OK
foo::bar(42); // OK
bar(42); // No ADL: error: 'bar' was not declared in this scope
}