How to fix expected primary-expression? - c++

#include<iostream>
#include<memory>
class test{
public:
void print()
{
std::cout<<"test print"<<std::endl;
}
};
int main
{
std::auto_ptr<test> t1 (new test);
t1->print();
return 0;
}
I am getting following error:
$g++ 5.cpp --std=c++11
5.cpp:16:22: error: expected primary-expression before ‘t1’
std::auto_ptr<test> t1 (new test);
^
5.cpp:16:22: error: expected ‘}’ before ‘t1’
5.cpp:16:22: error: expected ‘,’ or ‘;’ before ‘t1’
5.cpp:17:2: error: ‘t1’ does not name a type
t1->print();
^
5.cpp:19:2: error: expected unqualified-id before ‘return’
return 0;
^
5.cpp:20:1: error: expected declaration before ‘}’ token
}
^

int main // <-- Notice anything ?

the problem is, that you forgot the parantheses in main.
int main { ...} // this is wrong!
but the right would be
int main() { ... }

Related

Weird C++ error, unqualified ID, redeclaration

I want to create a project, I have 3 files, a test.cpp, something.h and a something.cpp. here they are:
test.cpp:
#include <bits/stdc++.h>
#define f cin
#define g cout
#include "something.h"
using namespace std;
int main(void)
{
int x;
register(x);
return 0;
}
something.h:
#ifndef __SOMETHING__H_
#define __SOMETHING__H_
#include <bits/stdc++.h>
void register(int x);
#endif
something.cpp:
#include "something.h"
void register(int x)
{
std::cout << x << '\n';
}
And here is the error I get:
In file included from test.cpp:4:0:
something.h:5:15: error: expected unqualified-id before ‘int’
void register(int x);
^~~
something.h:5:15: error: expected ‘)’ before ‘int’
test.cpp: In function ‘int main()’:
test.cpp:10:15: error: ISO C++ forbids declaration of ‘x’ with no type [-fpermissive]
register(x);
^
test.cpp:10:15: error: redeclaration of ‘int x’
test.cpp:9:9: note: ‘int x’ previously declared here
int x;
^
In file included from something.cpp:1:0:
something.h:5:15: error: expected unqualified-id before ‘int’
void register(int x);
^~~
something.h:5:15: error: expected ‘)’ before ‘int’
something.cpp:3:15: error: expected unqualified-id before ‘int’
void register(int x)
^~~
something.cpp:3:15: error: expected ‘)’ before ‘int’
Why does it tell me that I redefine x? When I just want to call register with it's value.
register is a reserved word in C++. Therefore, you have to give another (unreserved) name.
more information: Register keyword in C++ - Stack Overflow

Why am i getting missing template arguments before ‘(’ token while using priority queue?

I was implementing an algorithm using priority queue.
Here is my code
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int first[2]={2,-2};
int second[2]={1,-1};
vector<pair<pair<int,int>,int>>vec;
class compare{
public:
bool operator()(pair<pair<int,int>,int>a,pair<pair<int,int>,int>b)
{
return a.second>b.second;
}
};
int main() {
long long int a,b,c,d;
while(cin>>a>>b>>c>>d)
{
priority_queue(pair<pair<int,int>,int>,vector<pair<pair<int,int>,int>>,compare) q;
map<pair<int,int>,bool>visited;
map<pair<int,int>,int>dist;
map<pair<int,int>,pair<int,int>> parent;
for(int i=1;i<=9;i++)
for(int j=1;j<=9;j++)
{
dist[make_pair(i,j)]=INT_MAX;
visited[make_pair(i,j)]=false;
}
dist[make_pair(a,b)] = 0;
visited[make_pair(a,b)] = true;
q.push(make_pair(make_pair(a,b),0));
while(!q.empty())
{
pair<int,int> node = q.top().first;
int distance = q.top().second;
q.pop();
//followed by relaxation step
}
}
// your code goes here
return 0;
}
The problem is I am getting the following errors:
rog.cpp: In function ‘int main()’:
prog.cpp:39:17: error: missing template arguments before ‘(’ token
priority_queue(pair<pair<int,int>,int>,vector<pair<pair<int,int>,int>>,compare) q;
^
prog.cpp:39:41: error: expected primary-expression before ‘,’ token
priority_queue(pair<pair<int,int>,int>,vector<pair<pair<int,int>,int>>,compare) q;
^
prog.cpp:39:73: error: expected primary-expression before ‘,’ token
priority_queue(pair<pair<int,int>,int>,vector<pair<pair<int,int>,int>>,compare) q;
^
prog.cpp:39:73: error: expected primary-expression before ‘)’ token
priority_queue(pair<pair<int,int>,int>,vector<pair<pair<int,int>,int>>,compare) q;
I am not able to understand what exactly is the meaning of the error.It wold be of great help if someone could clarify it for me.
You are not using proper syntax. Instead of writing
priority_queue(pair<pair<int,int>,int>,vector<pair<pair<int,int>,int>>,compare) q;
You should write
priority_queue<pair<pair<int,int>,int>,vector<pair<pair<int,int>,int>>,compare> q;
For more information, refer to this link: http://en.cppreference.com/w/cpp/container/priority_queue
Change this:
priority_queue(pair<pair<int,int>,int>,vector<pair<pair<int,int>,int>>,compare) q;
to this:
priority_queue<pair<pair<int,int>,int>,vector<pair<pair<int,int>,int>>,compare> q;
and you will get this code compiled.
However, this code is unreadable. Consider using a typedef.

Am I compiling in the wrong way?

So I've got this RPN calculator project and I will try to post all the code for it; would I just run "C++ dc.cpp"? I get returned an error even though I know someone else has compiled it correctly. How do I compile this?
dc.cpp
#include <iostream>
#include "stack.h"
#include <string>
#include "dsexceptions.h"
using namespace std;
bool IsOperator(char op);
int calculate(char op, int operand1, int operand2);
int main() {
string input;
Stack<int> dc;
while (getline(cin, input))
{
for (int i = 0; i<input.length(); i++)
{
if (input[i] == ' ')
continue;
else if (IsOperator(input[i]))
{
try {
int operand2 = dc.top();
dc.pop();
int operand1 = dc.top();
dc.pop();
int result = calculate(input[i], operand1, operand2);
dc.push(result);
}
catch (Underflow e) {
cout << "No elements in stack";
}
catch (Overflow e) {
cout << "Stack full. Can't insert more";
}
catch (DivisionByZero e) {
cout << "Please choose some other value for division except 0";
}
catch (InvalidOperator e) {
cout << "The operator you choose is invalid";
}
}
else if (isdigit(input[i]))
{
int operand = 0;
while (i<input.length() && isdigit(input[i]))
{
operand = (operand * 10) + (input[i] - '0');
i++;
}
i--;
if (i && input[i - 1] == '_')
operand *= -1;
try {
dc.push(operand);
}
catch (Overflow e) {
cout << "Stack full. Can't insert more";
}
}
else if (input[i] == 'p') {
try {
cout << dc.top() << endl;
}
catch (Underflow e) {
cout << "No elements in stack";
}
}
else if (input[i] == 'n') {
try {
cout << dc.top();
}
catch (Underflow e) {
cout << "No elements in stack";
}
}
else if (input[i] == 'f') {
for (Stack <int> dump = dc; !dump.isEmpty(); dump.pop()) {
try {
cout << dump.top() << " ";
}
catch (Underflow e) {
cout << "No elements in stack";
}
}
cout << endl;
}
else if (input[i] == 'c') {
while (!dc.isEmpty())
dc.pop();
}
else if (input[i] == 'd') {
try {
dc.push(dc.top());
}
catch (Overflow e) {
cout << "Stack full. Can't insert more";
}
catch (Underflow e) {
cout << "No elements in stack";
}
}
else if (input[i] == 'r') {
try {
int x = dc.top();
dc.pop();
int y = dc.top();
dc.pop();
dc.push(x);
dc.push(y);
}
catch (Overflow e) {
cout << "Stack full. Can't insert more";
}
catch (Underflow e) {
cout << "No elements in stack";
}
}
}
}
return 0;
}
bool IsOperator(char op)
{
if (op == '+' || op == '-' || op == '*' || op == '/' || op == '%')
{
return true;
}
else
{
return false;
}
}
int calculate(char op, int operand1, int operand2)
{
if (op == '+')
{
return operand1 + operand2;
}
else if (op == '-')
{
return operand1 - operand2;
}
else if (op == '*')
{
return operand1 * operand2;
}
else if (op == '/')
{
return operand1 / operand2;
if (operand2 == 0)
{
throw DivisionByZero();
}
}
else if (op == '%')
{
return operand1 % operand2;
}
else {
throw InvalidOperator();
}
}
dsexceptions.h
#ifndef _DSEXCEPTIONS_H_
#define _DSEXCEPTIONS_H_
#include <iostream>
class Underflow : public std::runtime_error
{
public:
Underflow(std::string const& error)
: std::runtime_error(error) {}
Underflow()
:std::runtime_error("Underflow Exception") {}
};
class Overflow : public std::runtime_error
{
public:
Overflow(std::string const& error)
: std::runtime_error(error) {}
Overflow()
:std::runtime_error("Overflow Exception") {}
};
class InvalidOperator : public std::runtime_error
{
public:
InvalidOperator(std::string const& error)
: std::runtime_error(error) {}
InvalidOperator()
:std::runtime_error("Invalid Exception") {}
};
class OutOfMemory : public std::runtime_error
{
public:
OutOfMemory(std::string const& error)
: std::runtime_error(error) {}
OutOfMemory()
:std::runtime_error("Out Of Memory Exception") {}
};
class BadIterator : public std::runtime_error
{
public:
BadIterator(std::string const& error)
: std::runtime_error(error) {}
BadIterator()
:std::runtime_error("Bad Iterator Exception") {}
};
class DataError : public std::runtime_error
{
public:
DataError(std::string const& error)
: std::runtime_error(error) {}
DataError()
:std::runtime_error("Data Error Exception") {}
};
class DivisionByZero : public std::runtime_error
{
public:
DivisionByZero(std::string const& error)
: std::runtime_error(error) {}
DivisionByZero()
:std::runtime_error("Division By Zero Exception") {}
};
#endif
stack.cpp
#include "dsexceptions.h"
#include "stack.h"
/**
* Construct the stack.
*/
template <class Object>
Stack<Object>::Stack(int aCapacity)
{
topOfStack = -1;
capacity = aCapacity;
theArray.resize(aCapacity);
}
/**
* Test if the stack is logically empty.
* Return true if empty, false otherwise.
*/
template <class Object>
bool Stack<Object>::isEmpty() const
{
return topOfStack == -1;
}
/**
* Test if the stack is logically full.
* Return true if full, false otherwise.
*/
template <class Object>
bool Stack<Object>::isFull() const
{
return topOfStack == capacity - 1;
}
/**
* Make the stack logically empty.
*/
template <class Object>
void Stack<Object>::makeEmpty()
{
topOfStack = -1;
}
/**
* Get the most recently inserted item in the stack.
* Does not alter the stack.
* Return the most recently inserted item in the stack.
* Exception Underflow if stack is already empty.
*/
template <class Object>
const Object & Stack<Object>::top() const
{
if (isEmpty())
throw Underflow();
return theArray[topOfStack];
}
/**
* Remove the most recently inserted item from the stack.
* Exception Underflow if stack is already empty.
*/
template <class Object>
void Stack<Object>::pop()
{
if (isEmpty())
throw Underflow();
topOfStack--;
}
/**
* Insert x into the stack, if not already full.
* Exception Overflow if stack is already full.
*/
template <class Object>
void Stack<Object>::push(const Object & x)
{
if (isFull())
throw Overflow();
theArray[++topOfStack] = x;
}
/**
* Return and remove most recently inserted item from the stack.
* Return most recently inserted item.
* Exception Underflow if stack is already empty.
*/
template <class Object>
Object Stack<Object>::topAndPop()
{
if (isEmpty())
throw Underflow();
return theArray[topOfStack--];
}
/* int main()
{
cout << "testing stack class" << endl;
Stack<int> dc(100);
cout << " stack class init" << endl;
dc.push(10);
dc.pop();
cout << "done testing stack class" << endl;
}*/
My compiler returns these errors when I run "c++ dc.cpp". I have to just be compiling it incorrectly, right?:
In file included from stack.cpp:1:
dsexceptions.h:8: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘Underflow::Underflow(const std::string&)’:
dsexceptions.h:11: error: expected class-name before ‘(’ token
dsexceptions.h:11: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘Underflow::Underflow()’:
dsexceptions.h:14: error: expected class-name before ‘(’ token
dsexceptions.h:14: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:18: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘Overflow::Overflow(const std::string&)’:
dsexceptions.h:22: error: expected class-name before ‘(’ token
dsexceptions.h:22: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘Overflow::Overflow()’:
dsexceptions.h:25: error: expected class-name before ‘(’ token
dsexceptions.h:25: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:29: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘InvalidOperator::InvalidOperator(const std::string&)’:
dsexceptions.h:33: error: expected class-name before ‘(’ token
dsexceptions.h:33: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘InvalidOperator::InvalidOperator()’:
dsexceptions.h:36: error: expected class-name before ‘(’ token
dsexceptions.h:36: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:42: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘OutOfMemory::OutOfMemory(const std::string&)’:
dsexceptions.h:46: error: expected class-name before ‘(’ token
dsexceptions.h:46: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘OutOfMemory::OutOfMemory()’:
dsexceptions.h:49: error: expected class-name before ‘(’ token
dsexceptions.h:49: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:53: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘BadIterator::BadIterator(const std::string&)’:
dsexceptions.h:57: error: expected class-name before ‘(’ token
dsexceptions.h:57: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘BadIterator::BadIterator()’:
dsexceptions.h:60: error: expected class-name before ‘(’ token
dsexceptions.h:60: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:64: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘DataError::DataError(const std::string&)’:
dsexceptions.h:68: error: expected class-name before ‘(’ token
dsexceptions.h:68: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘DataError::DataError()’:
dsexceptions.h:71: error: expected class-name before ‘(’ token
dsexceptions.h:71: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:75: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘DivisionByZero::DivisionByZero(const std::string&)’:
dsexceptions.h:79: error: expected class-name before ‘(’ token
dsexceptions.h:79: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘DivisionByZero::DivisionByZero()’:
dsexceptions.h:82: error: expected class-name before ‘(’ token
dsexceptions.h:82: error: expected ‘{’ before ‘(’ token
In file included from stack.cpp:2:
stack.h: At global scope:
stack.h:1: error: expected constructor, destructor, or type conversion before ‘<’ token
stack.h:34: error: ISO C++ forbids declaration of ‘vector’ with no type
stack.h:34: error: expected ‘;’ before ‘<’ token
stack.h:36: error: ISO C++ forbids declaration of ‘vector’ with no type
stack.h:36: error: expected ‘;’ before ‘<’ token
stack.cpp: In constructor ‘Stack<Object>::Stack(int)’:
stack.cpp:11: error: ‘theArray’ was not declared in this scope
stack.cpp: In member function ‘const Object& Stack<Object>::top() const’:
stack.cpp:54: error: ‘theArray’ was not declared in this scope
stack.cpp: In member function ‘void Stack<Object>::push(const Object&)’:
stack.cpp:78: error: ‘theArray’ was not declared in this scope
stack.cpp: In member function ‘Object Stack<Object>::topAndPop()’:
stack.cpp:91: error: ‘theArray’ was not declared in this scope
[jones_g#cobra Prog2]$ c++ dc.cpp
In file included from dc.cpp:2:
stack.h:1: error: expected constructor, destructor, or type conversion before ‘<’ token
stack.h:34: error: ISO C++ forbids declaration of ‘vector’ with no type
stack.h:34: error: expected ‘;’ before ‘<’ token
stack.h:36: error: ISO C++ forbids declaration of ‘vector’ with no type
stack.h:36: error: expected ‘;’ before ‘<’ token
In file included from dc.cpp:4:
dsexceptions.h:8: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘Underflow::Underflow(const std::string&)’:
dsexceptions.h:11: error: expected class-name before ‘(’ token
dsexceptions.h:11: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘Underflow::Underflow()’:
dsexceptions.h:14: error: expected class-name before ‘(’ token
dsexceptions.h:14: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:18: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘Overflow::Overflow(const std::string&)’:
dsexceptions.h:22: error: expected class-name before ‘(’ token
dsexceptions.h:22: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘Overflow::Overflow()’:
dsexceptions.h:25: error: expected class-name before ‘(’ token
dsexceptions.h:25: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:29: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘InvalidOperator::InvalidOperator(const std::string&)’:
dsexceptions.h:33: error: expected class-name before ‘(’ token
dsexceptions.h:33: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘InvalidOperator::InvalidOperator()’:
dsexceptions.h:36: error: expected class-name before ‘(’ token
dsexceptions.h:36: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:42: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘OutOfMemory::OutOfMemory(const std::string&)’:
dsexceptions.h:46: error: expected class-name before ‘(’ token
dsexceptions.h:46: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘OutOfMemory::OutOfMemory()’:
dsexceptions.h:49: error: expected class-name before ‘(’ token
dsexceptions.h:49: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:53: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘BadIterator::BadIterator(const std::string&)’:
dsexceptions.h:57: error: expected class-name before ‘(’ token
dsexceptions.h:57: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘BadIterator::BadIterator()’:
dsexceptions.h:60: error: expected class-name before ‘(’ token
dsexceptions.h:60: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:64: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘DataError::DataError(const std::string&)’:
dsexceptions.h:68: error: expected class-name before ‘(’ token
dsexceptions.h:68: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘DataError::DataError()’:
dsexceptions.h:71: error: expected class-name before ‘(’ token
dsexceptions.h:71: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:75: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘DivisionByZero::DivisionByZero(const std::string&)’:
dsexceptions.h:79: error: expected class-name before ‘(’ token
dsexceptions.h:79: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘DivisionByZero::DivisionByZero()’:
dsexceptions.h:82: error: expected class-name before ‘(’ token
dsexceptions.h:82: error: expected ‘{’ before ‘(’ token
[jones_g#cobra Prog2]$ c++ dc.cpp
In file included from dc.cpp:2:
stack.h:1: error: expected constructor, destructor, or type conversion before ‘<’ token
stack.h:34: error: ISO C++ forbids declaration of ‘vector’ with no type
stack.h:34: error: expected ‘;’ before ‘<’ token
stack.h:36: error: ISO C++ forbids declaration of ‘vector’ with no type
stack.h:36: error: expected ‘;’ before ‘<’ token
In file included from dc.cpp:4:
dsexceptions.h:8: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘Underflow::Underflow(const std::string&)’:
dsexceptions.h:11: error: expected class-name before ‘(’ token
dsexceptions.h:11: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘Underflow::Underflow()’:
dsexceptions.h:14: error: expected class-name before ‘(’ token
dsexceptions.h:14: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:18: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘Overflow::Overflow(const std::string&)’:
dsexceptions.h:22: error: expected class-name before ‘(’ token
dsexceptions.h:22: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘Overflow::Overflow()’:
dsexceptions.h:25: error: expected class-name before ‘(’ token
dsexceptions.h:25: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:29: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘InvalidOperator::InvalidOperator(const std::string&)’:
dsexceptions.h:33: error: expected class-name before ‘(’ token
dsexceptions.h:33: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘InvalidOperator::InvalidOperator()’:
dsexceptions.h:36: error: expected class-name before ‘(’ token
dsexceptions.h:36: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:42: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘OutOfMemory::OutOfMemory(const std::string&)’:
dsexceptions.h:46: error: expected class-name before ‘(’ token
dsexceptions.h:46: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘OutOfMemory::OutOfMemory()’:
dsexceptions.h:49: error: expected class-name before ‘(’ token
dsexceptions.h:49: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:53: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘BadIterator::BadIterator(const std::string&)’:
dsexceptions.h:57: error: expected class-name before ‘(’ token
dsexceptions.h:57: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘BadIterator::BadIterator()’:
dsexceptions.h:60: error: expected class-name before ‘(’ token
dsexceptions.h:60: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:64: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘DataError::DataError(const std::string&)’:
dsexceptions.h:68: error: expected class-name before ‘(’ token
dsexceptions.h:68: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘DataError::DataError()’:
dsexceptions.h:71: error: expected class-name before ‘(’ token
dsexceptions.h:71: error: expected ‘{’ before ‘(’ token
dsexceptions.h: At global scope:
dsexceptions.h:75: error: expected class-name before ‘{’ token
dsexceptions.h: In constructor ‘DivisionByZero::DivisionByZero(const std::string&)’:
dsexceptions.h:79: error: expected class-name before ‘(’ token
dsexceptions.h:79: error: expected ‘{’ before ‘(’ token
dsexceptions.h: In constructor ‘DivisionByZero::DivisionByZero()’:
dsexceptions.h:82: error: expected class-name before ‘(’ token
dsexceptions.h:82: error: expected ‘{’ before ‘(’ token
It looks like you are compiling it fine, but your code has errors, which your compiler is telling you about.
At the very least, add...
#include <stdexcept>
... to your dsexceptions.h file.
You might also need to add...
#include <vector>
... to your stack.h file.
You need to read the error message more carefully. Taking just the first message:
dsexceptions.h:8: error: expected class-name before ‘{’ token
You can see that the compiler doesn't recognise the token immediately preceding {. What's that token? Line 7 is
class Underflow : public std::runtime_error
Yes, you're using std::runtime_error, but haven't defined it. The fix, of course, is to #include <stdexcept> instead of the unnecessary #include <iostream>.
In your headers you should normally include any definitions that the header needs (and only what the header needs). If you don't, then you are likely to end up with very fragile code where headers fail as above if the code using them doesn't know the prerequisites. It's okay to include the standard library headers multiple times, and the same is true of any well-written library header.
There are tools, such as include-what-you-use (aka iwyu) to help analyse your source files for missing or unnecessary includes - you should certainly look to see if that might help you.

Compilation error with struct/class defined in a function

What is the problem with the following code. Because if I define the class inside the main function, the compilation fails and I don't understand the compiler error.
Test the code from here
Comment the 1st definition of drift_f (outside of main()) and uncomment the inner definition of drif_t (inside the main() function) and the compiler will get the following error message:
prog.cpp: In function ‘int main()’:
prog.cpp:27:24: error: template argument for ‘template<class> class std::allocator’ uses local type ‘main()::drift_t’
std::deque<drift_t> drift; drift.push_back(drift_t(0,0));drift.push_back(drift_t(-50,-50));
^
prog.cpp:27:24: error: trying to instantiate ‘template<class> class std::allocator’
prog.cpp:27:24: error: template argument 2 is invalid
prog.cpp:27:31: error: invalid type in declaration before ‘;’ token
std::deque<drift_t> drift; drift.push_back(drift_t(0,0));drift.push_back(drift_t(-50,-50));
^
prog.cpp:27:39: error: request for member ‘push_back’ in ‘drift’, which is of non-class type ‘int’
std::deque<drift_t> drift; drift.push_back(drift_t(0,0));drift.push_back(drift_t(-50,-50));
^
prog.cpp:27:69: error: request for member ‘push_back’ in ‘drift’, which is of non-class type ‘int’
std::deque<drift_t> drift; drift.push_back(drift_t(0,0));drift.push_back(drift_t(-50,-50));
#include <iostream>
#include <deque>
using namespace std;
class drift_t{ //It works
public:
int _drift;
int _immediateDrift;
drift_t() : _drift(0), _immediateDrift(0) {}
drift_t(int d, int expected) : _drift(d), _immediateDrift(expected) {}
drift_t(const drift_t& ro) : _drift(ro._drift), _immediateDrift(ro._immediateDrift) {}
drift_t& operator = (const drift_t& ro) { this->_drift = ro._drift; this->_immediateDrift = ro._immediateDrift; return *this; }
} ;//*/
int main() {
/*class drift_t{ //It doesn't works
public:
int _drift;
int _immediateDrift;
drift_t() : _drift(0), _immediateDrift(0) {}
drift_t(int d, int expected) : _drift(d), _immediateDrift(expected) {}
drift_t(const drift_t& ro) : _drift(ro._drift), _immediateDrift(ro._immediateDrift) {}
drift_t& operator = (const drift_t& ro) { this->_drift = ro._drift; this->_immediateDrift = ro._immediateDrift; return *this; }
} ;//*/
std::deque<drift_t> drift; drift.push_back(drift_t(0,0));drift.push_back(drift_t(-50,-50));
return 0;
}
Isn't the error the one that the compiler says it is? You can't use the local class for that template initialization.
Try compiling with -std=c++11 as I believe it relaxes on that.

Trouble compiling a simple C++0x program with lambdas

I am trying to run a simple lambda example.
// lambda.cpp
#include <functional>
//#include <tr1/functional>
int main()
{
// Assign the same lambda expression to a function object.
function<int (int, int)> f2 = [] (int x, int y) { return x + y; };
//function<int (int, int)> f2 = [] (int x, int y) { return x + y; };
}
I'm compiling it like this:
$ g++ -std=c++0x -fpermissive lamdas.cpp
lambdas.cpp: In function ‘int main()’:
lambdas.cpp:10: error: expected primary-expression before ‘=’ token
lambdas.cpp:10: error: expected primary-expression before ‘[’ token
lambdas.cpp:10: error: expected primary-expression before ‘]’ token
lambdas.cpp:10: error: expected primary-expression before ‘int’
lambdas.cpp:10: error: expected primary-expression before ‘int’
lambdas.cpp:10: error: expected ‘;’ before ‘{’ token
How do I get it to compile with no errors?
Did you mean std::function?
Standard library features live in the std namespace.
It's also interesting that your copy/paste is clearly fake; you wrote "lamdas.cpp" then compiled "lambdas.cpp"!
std::function<int (int, int)> f2 = [] (int x, int y) { return x + y; };
or, probably better
auto f2 = [] (int x, int y) { return x + y; };
It looks to me like you forgot -std=c++0x.