Reputation: 13
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;
}
'''
Upvotes: 0
Views: 894
Reputation: 13
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!
Upvotes: 0
Reputation: 51
There's a lot of issues with the code that prevent from compilation:
fibon_seq
and is_size_ok
are not defined. You have to define them in this file or include them with a #define
macro.std
namespace. You need to include this with using namespace std;
, usually at the beginning of the code, after library includes.msg
is passed as an argument in a function, but it's not defined.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
.Upvotes: 1
Reputation: 19349
There are a few problems with your code:
ocnst
should be const
.#include <string>
#include <vector>
The last one is the likely cause of that particular error.
Upvotes: 0