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

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!

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

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;
}

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.