exepted identifier or '(' before '{' token error while compiling - list

This might be a very stupid problem but i can't find a way to solve it.
i'm getting this error message while trying to compile this program:
Exercise1.c:28.1:exepted identifier or '(' before '{' token.
Can you please explain what am i doing wrong? Thanks.
#include <stdio.h>
#include <stdlib.h>
typedef struct II_node_struct
{int info;
struct II_node_struct *next;}II_node;
II_node* push(II_node* lis,int);
II_node* pop(II_node* lis);
int main()
{
II_node* pila;
II_node* corr=pila;
int x;
while(x>=0)
{scanf(" %d",&x);
if(x>0)
corr=push(corr,x);
if(x==0)
corr=pop(corr);}
while(corr->next!=NULL)
{printf("%d\n",corr->info);
corr=corr->next;}
return 0;
}
II_node* pop(II_node* lis);
{
II_node* risultato;
if(lis==NULL)
risultato=lis;
if(lis->next==NULL)
{risultato=lis;
free(lis);}
else
{risultato=lis->next;
free(lis);}
return risultato;
}
II_node* push(II_node* lis,int x)
{
II_node* new=malloc(sizeof(II_node));
if(lis==NULL)
{lis=new;
new->next=NULL;
new->info=x;}
else
{new->next=lis;
lis=new;}
return lis;
}

Related

C language if else uotput question logic unable to understand

#include <stdio.h>
void main()
{
int x = 5;
if (x < 1);
printf("Hello");
}
how it output hello
i expected error but there was no eroor
Because you have ';' after the "(x<1)"
Remove that semicolon and it will work.

C++ Expected unqualified-id when trying to write a function

I wrote this code and was just trying to continue writing a function called register. I choose to set it to void because i am not certain if i am going to return anything at the moment. The Expected unqualified-id seems to show up when you put a semicolon in the wrong place, but i cant see that i make that error here..
I tried to comment out the menu.h include and all the three functions i get from the header file. No difference.
#include <iostream>
#include <string>
#include "menu.h"
//From the menu.h header file
int mainMenu();
int customerMenu();
int librarianMenu();
void register() { //Here the error appears.
}
int main () {
int runFlag = 1;
while(runFlag == 1) {
int mainMenuChoice = mainMenu();
if (mainMenuChoice == 1) {
customerMenu();
}
else if(mainMenuChoice == 2) {
librarianMenu();
}
}
}

How to understand C++ error, "no match for 'operator==' (operand types are 'std::pair' and 'const int')"?

I am compiling the following code on codeblocks and I get the following error statement
C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\predefined_ops.h|191|error: no match for 'operator==' (operand types are 'std::pair' and 'const int')
Also an error is displayed in the header file predefined_ops.h:
template<typename _Iterator>
bool
operator()(_Iterator __it)
{ return *__it == _M_value; }//error
};
This is the code which I am compiling
#include <bits/stdc++.h>
using namespace std;
class Soham
{
int *a,n;
map<int,int> m;
public:
Soham(int x);
void search1(int,int,int,int);
};
Soham::Soham(int x)
{
n=x;
a=new int[n];
for(int i=0;i<n;i++)
cin>>a[i];
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(abs(a[i]-a[j])<=1)
{
search1(a[i],a[j],i,j);
}
}
}
map<int,int> ::iterator it1;
for(it1=m.begin();it1!=m.end();it1++)
{
cout<<it1->first<<"-->"<<it1->second<<endl;
}
}
void Soham::search1(int x,int y,int i1,int j1)
{
if(m.empty())
{
m.insert(std::pair<int,int>(x,i1));
m.insert(std::pair<int,int>(y,j1));
}
else
{
map<int,int>::iterator it,it1;
it=find(m.begin(),m.end(),x);
it1=find(m.begin(),m.end(),y);
if(it!=m.end()|| it1!=m.end())
{
if (it!=m.end() && it->second!=i1)//chance of error
{
m.insert(std::pair<int,int>(it->first,i1));
}
if(it1!=m.end() && it1->second!=j1)//chance of error
{
m.insert(std::pair<int,int>(it1->first,j1));
}
}
//find failed to find element in the map how to show this particular condition
else //error
{
if(it!=m.end())
{
m.insert(std::pair<int,int>(x,i1));
}
if(it1!=m.end())
{
m.insert(std::pair<int,int>(y,j1));
}
}
}
}
int main()
{
int n;
cin>>n;
Soham a(n);
return 0;
}
As per the error statement i am doing an invalid comparison using the == operator, but i dont get get it
this is where most probably the error occurs in the following conditions
if (it!=m.end() && it->second!=i1)
if(it1!=m.end() && it1->second!=j1)
In the second check I am checking the second element of the pair(it->second) which is of type int with an integer variable i1 then why is that the error occurs with the == operator. I may have understood the error in the wrong manner and accordingly explained my understanding, if that's not the case. What generates the error and how to rectify it?
Change the following lines and it will run
//it=find(m.begin(),m.end(),x);
it = m.find(x);
//it1=find(m.begin(),m.end(),y);
it1 = m.find(y);
Basically you have to use the find member function instead of the
find algorithm.

for(auto& x : unordered_map variable) - this statement throwing error

Hi my code snippet is as below
#include <iostream>
#include <string>
#include <unordered_map>
struct job
{
int priority;
int state;
std::string name;
};
job* selectJob(std::unordered_map<int, job*> jobList)
{
for (auto& x : jobList)
{
if(x->state == 1)
return x;
}
return NULL;
}
int main()
{
std::unordered_map<int, job*> jobList;
job a = { 1, 1, "a" };
jobList.insert(std::make_pair<int, job*>(1, &a));
job *selected = NULL;
while (NULL != (selected = selectJob(jobList)))
{
std::cout << "Name: " << selected->name << "Prio: " << selected->priority << std::endl;
selected->state = 2;
}
return 0;
}
When compiled on linux it is throwing an error:
g++ -std=gnu++0x q.cpp
q.cpp: In function âjob* selectJob(std::unordered_map<int, job*, std::hash<int>, std::equal_to<int>, std::allocator<std::pair<const int, job*> > >&)â:
q.cpp:13: error: a function-definition is not allowed here before â:â token
q.cpp:18: error: expected primary-expression before âreturnâ
q.cpp:18: error: expected `;' before âreturnâ
q.cpp:18: error: expected primary-expression before âreturnâ
q.cpp:18: error: expected `)' before âreturnâ
Has anyone faced this issue?
The compiler version you're using (gcc 4.3) does not support auto variables.
http://gcc.gnu.org/gcc-4.3/cxx0x_status.html
auto-typed variables N1984 No
the value_type of an unordered_map is a pair of the key and value, you need to select the second.
job* selectJob(std::unordered_map<int, job*> jobList)
{
for (auto& x : jobList)
{
if(x->second->state == 1)
return x->second;
}
return NULL;
}
Also you might want to pass by reference to avoid copying and leading to a dangling point.
job* selectJob(std::unordered_map<int, job*>& jobList)

need help on #include doesn't seem to be working

So I have a class called HPStack and I have to include it in my main class etc. However I get a "In File included from" error, what could be causing this?
Also my string objects also have errors I have have no idea why, the error is: "Unable to identifier string".
I'm new the C++ so any help would be appreciated, thanks in advance.
The error I am getting (I think) are these:
error: expected unqualified-id before "namespace"
error: expected `,' or `;' before "namespace"
error: expected namespace-name before ';' token
error: `<type error>' is not a namespace
Im not sure what I am missing but that isn't telling me much.
Here is my code: The class.h file.
#ifndef HPSTACK_H
#define HPSTACK_H
class HPStack {
public:
HPStack();
void push(double);
double pop();
double peek();
private:
double register_[4];
}
#endif
The class.cpp file.
#include "HPStack.h"
#include <cstdlib>
HPStack::HPStack() : register_{}{
}
double HPStack::push(double x) {
for (int i = 2; i >= 0; i--) {
if (isdigit(register_[i])) {
register_[i] = register_[i + 1];
}
register_[0] = x;
}
}
double HPStack::pop() {
return register_[0];
for (int i = 0; i < 3; i++) {
register_[i] = register_[i + 1];
}
}
double HPStack::peek() {
return register_[0];
}
And my main file:
#include "HPStack.h"
#include <cstdlib>
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
int main() {
HPStack stack;
string line;
while (getline(cin, line)) {
stringstream expression(line);
string token;
while (expression >> token) {
if (isdigit(token[0])) {
stack.push(atof(token.data()));
} else if (token == "+") {
double x = stack.pop();
double y = stack.pop();
double z = (y + x);
stack.push(z);
}
}
cout << stack.peek();
}
The error is, I'm guessing, because of this line:
double register_[4] = {};
You can not initialize class members when declaring them.
If your compiler is new enough to support C++11 features, you can use an initializer list with the constructor:
HPStack::HPStack()
: register_{}
{
}
Otherwise you have to initialize the array manually in the constructor.
And as I noted in a comment, using register_ - 2 makes no sense as it returns a pointer so the index variable i will be way beyond the end of the array.
And using register_ - 1 as the condition in the pop loop makes even less sense, as it will always be non-zero and therefore always true and the loop will loop forever.
You're missing the ; at the end of the class definition:
class HPStack {
...
}; // <== This semicolon is required