Error expected declaration before token

Списки. Ошибка: expected declaration before '}' token C++ Решение и ответ на вопрос 897696

Lex0R

0 / 0 / 0

Регистрация: 28.03.2013

Сообщений: 5

1

10.06.2013, 19:08. Показов 23846. Ответов 4

Метки нет (Все метки)


Добрый день, пытаюсь освоить списки, пока почти ничего ещё не понял. Вообщем собираюсь сделать несколько элементов списка, так же поиск и удаление элементов, до удаления ещё не дошёл.
В данный момент программа ругается на поиск в списке, точнее на последнюю строку, она же «}», подскажите что не так?

C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream.h>
#include <conio.h>
 
using namespace std;
 
struct list
{ 
   int id;    
   int first;
   int second;
   list *next;
}; 
 
int main ()
{   
   
   char a; 
   int i;
   int n;                               // Êîë-âî ñïèñêîâ
   list *phead, *t;
 
   phead = new (list);
   t = phead;
////////////////////////////////////////   
 
   while (i!=5) 
   {                                     //Ïðîâåðêà íà êîë-âî ââîäîâ
   cout<<"Number of list's: ";  
   cin>>n;
   
   if (n<1)
      {
           cout<<"Error, try again!"<<endl;
           getch();
           system("cls"); 
      }
      else
           {break;}
   }
   
   cout<<endl;
////////////////////////////////////////
   
   for (i=1; i<n; i++)                  //Ââîäèì ýëåìåíòû
   {
       (*t).id=i;
       cout<<"Enter First: ";
       cin>>(*t).first;
       cout<<"Enter Second: ";
       cin>>(*t).second;
       cout<<endl;
       (*t).next = new (list);
       t = (*t).next;
   }
   
   (*t).id=n;                         //Ïîñëåäíèé ýëåìåíò ñïèñêà
   cout<<"Enter First: ";
   cin>>(*t).first;
   cout<<"Enter Second: ";
   cin>>(*t).second;
   cout<<endl;
   (*t).next = new (list);
   (*t).next = NULL;
   
   system("cls");
//////////////////////////////////////
   
   for (t=phead; t!=NULL; t=(*t).next)
   {
       cout<<"ID: "<<(*t).id<<endl;
       cout<<"Adress: "<<t<<endl;
       cout<<"First: "<<(*t).first<<endl;
       cout<<"Second: "<<(*t).second<<endl;
       cout<<endl;
   }
//////////////////////////////////////
   
   cout<<"Search?"<<endl;
   
   a=getch();
   
   if ((a=='y') || (a=='Y')) 
   {
   system("cls");
   cout<<"Enter ID: ";}
   cin>>n;
          
          for (t=phead; t!=NULL; t=(*t).next)
          { 
              if ( (*t).id==n )
                {system("cls");            
                 cout<<"Your element: "<<endl<<endl;
                 cout<<"ID: "<<(*t).id<<endl;
                 cout<<"Adress: "<<t<<endl;
                 cout<<"First: "<<(*t).first<<endl;
                 cout<<"Second: "<<(*t).second<<endl;}  
          }      
          
   }
}

__________________
Помощь в написании контрольных, курсовых и дипломных работ, диссертаций здесь



0



Croessmah

Don’t worry, be happy

17781 / 10545 / 2036

Регистрация: 27.09.2012

Сообщений: 26,516

Записей в блоге: 1

10.06.2013, 19:20

2

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
   if ((a=='y') || (a=='Y')) 
   {//1
   system("cls");
   cout<<"Enter ID: ";}//0
   cin>>n;
          
          for (t=phead; t!=NULL; t=(*t).next)
          { //1
              if ( (*t).id==n )
                {system("cls");  //2          
                 cout<<"Your element: "<<endl<<endl;
                 cout<<"ID: "<<(*t).id<<endl;
                 cout<<"Adress: "<<t<<endl;
                 cout<<"First: "<<(*t).first<<endl;
                 cout<<"Second: "<<(*t).second<<endl;}  //1
          }      //0
          
   }//-1 - откуда она?



1



Lex0R

0 / 0 / 0

Регистрация: 28.03.2013

Сообщений: 5

10.06.2013, 19:23

 [ТС]

3

Цитата
Сообщение от Croessmah
Посмотреть сообщение

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
   if ((a=='y') || (a=='Y')) 
   {//1
   system("cls");
   cout<<"Enter ID: ";}//0
   cin>>n;
          
          for (t=phead; t!=NULL; t=(*t).next)
          { //1
              if ( (*t).id==n )
                {system("cls");  //2          
                 cout<<"Your element: "<<endl<<endl;
                 cout<<"ID: "<<(*t).id<<endl;
                 cout<<"Adress: "<<t<<endl;
                 cout<<"First: "<<(*t).first<<endl;
                 cout<<"Second: "<<(*t).second<<endl;}  //1
          }      //0
          
   }//-1 - откуда она?

Тьфу ты блин, я уже минут 20 сижу эти скобки считаю, проглядел. Спасибо большое.

Ещё вопрос можно?
Не подскажете где можно про списки почитать? В интернете как-то странно всё разжёвано, полагаю надо искать в книгах, если да, то в каких?



0



Don’t worry, be happy

17781 / 10545 / 2036

Регистрация: 27.09.2012

Сообщений: 26,516

Записей в блоге: 1

10.06.2013, 19:29

4

Можете почитать книгу Уильям Топп, Уильям Форд «Структуры данных в C++»



1



0 / 0 / 0

Регистрация: 28.03.2013

Сообщений: 5

10.06.2013, 19:38

 [ТС]

5

Цитата
Сообщение от Croessmah
Посмотреть сообщение

Можете почитать книгу Уильям Топп, Уильям Форд «Структуры данных в C++»

Благодарю за помощь.



0



Expected declaration before token ‘}’

HI all

Please help with this problem. I think its quite small and I have been looking at it for ages but cant work it out?? The error I get in the code is as per the title of this message and it appears in line 13:1, at the end of the first function. It is a compile time error. I think its to do with brackets but I have tried as many combinations as I can think of with no success. The code is below. If anyone can help me out I would be grateful.

#include <iostream>
#include <vector>
using namespace std;

template<typename T>
void Print(T array[], int array_size)
{
for(int i = 0; i < array_size; i++)

cout <<array[i] <<» «;

}
}
template<typename T>
void print2(T vector<int> & v)
{
for (int i=1; i<=v.size(); i++) v.push_back(i);
{
std::cout << «myvector contains:»;
for (std::vector<int>::const iterator it = v.begin() ; it != v.end(); ++it){
std::cout << ‘ ‘ << *it;
std::cout << ‘n’;
}
}

}
int main (){

//double Array[10] = {1.3,2.44,0,3,4,2,5,6,0,7};
//Print(Array, 10);

string s[] = {«if», «you»,»just»,»think»,»about»,»it»,»it’s»,»actually»,»quite», «easy»};
Print(s, 10);

//int vector[3] = {1,2,3};
//print2(vector);

}

Remove the bracket on line 13.

if you indent your code, you will quickly see where is the brace error :

( and remove std:: prefixes since, you have using namespace std; )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <vector>
using namespace std;

template<typename T>
void Print(T array[], int array_size)
{
    for(int i = 0; i < array_size; i++)
        cout <<array[i] <<" ";

}

template<typename T>
void print2(T vector<int> & v)
{ 
    for (int i=1; i<=v.size(); i++) v.push_back(i);
    {
        std::cout << "myvector contains:";
        for ( std::vector<int>::const iterator it = v.begin() ;
                it != v.end();
                ++it)
        {
            std::cout << ' ' << *it;
            std::cout << 'n';
        }
    }	
}

int main ()
{

    //double Array[10] = {1.3,2.44,0,3,4,2,5,6,0,7};
    //Print(Array, 10);

    string s[] = {"if", "you","just","think","about","it","it's","actually","quite", "easy"};
    Print(s, 10);


    //int vector[3] = {1,2,3};
    //print2(vector);


}

Hi Fran

Thanks for the prompt reply.

I have tried this previously and this then brings up three more errors relating to line 15:27.
expected unqualified id before & token
expected ) before & token
expected initializer before & token.

Any ideas?

Hi Shadow fiend,

I removed a bracket from the original code but it brought up the three errors I listed to you. Im very new to this C++ so I cant work it out? When I add the backet I get one error when I take it out I get three? Also, if I want to use the std:: prefix do I put using std::cout and std::vector at the top instead of using namespace std?

Hi bennyscammin1

There are a few problems with the code,

#include <iostream>
#include <vector>
using namespace std;

template<typename T>
void Print(T array[], int array_size)
{
for(int i = 0; i < array_size; i++)
cout <<array[i] <<» «;
}

template<typename T>
void print2(const vector<T>& v)
{
for (int i=1; i<=v.size(); i++) v.push_back(i);
{
cout << «myvector contains:»;
for (const::vector<int>::iterator it = v.begin() ; it != v.end(); ++it)
{
cout << ‘ ‘ << *it;
cout << ‘n’;
}
}
}

int main ()
{

//double Array[10] = {1.3,2.44,0,3,4,2,5,6,0,7};
//Print(Array, 10);

string s[] = {«if», «you»,»just»,»think»,»about»,»it»,»it’s»,»actually»,»quite», «easy»};
Print(s, 10);
system («pause»);

//int vector[3] = {1,2,3};
//print2(vector);

}

Its always good to use using namespace std because by using that only you will be free to use std:: at every required place in your program.

That will not only reduce size of the program but also helps you to make any mistake related to std::

No Joy with that. Its now throwing up a full page of errors? Did the code above compile for you?

Thanks

Thanks. I will look into it further. There must be some issue with my compiler.

Topic archived. No new replies allowed.

@zekageri

In first compile with the librari :
expected declaration before ‘}’ token in the esp32ModbusTypeDefs.h line 66.

@bertmelis

I can’t see where this error comes from…
How does your file compare to the file in this repo?
Are there any more error messages?

Remark: I had some problems with this lib and am rewriting completely from scratch.

@zekageri

Iam using the Example code from github. I dont write anything else yet.
Just included the lib header and copy paste the rest just to see if it is
compile or not. But it is not. :(
I wanted to communicate with a PLC via modbus with this lib.

Bert Melis <notifications@github.com> ezt írta (időpont: 2019. aug. 14.,
Sze 9:20):

I can’t see where this error comes from…
How does your file compare to the file in this repo?
Are there any more error messages?

Remark: I had some problems with this lib and am rewriting completely from
scratch.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#6?email_source=notifications&email_token=AJBE42IFUKBNKPNQZMN66ZDQEOW5LA5CNFSM4ILRYHPKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD4H5TBQ#issuecomment-521132422>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AJBE42KVJYKKCNUUT7HGVADQEOW5LANCNFSM4ILRYHPA>
.

@zekageri

Yes, i have got some more errors but i think all of it are related to this error.
Other error messages are two include errors. the stdint.h and the functional.
I don’t know why these are here. I’am using PlatformIO ide with vscode and latest esp32 core in it with latest async lib.

@bertmelis

You mean you cannot include stdint and functional? Well, if these things are not included, the file will not compile.

Surprising however because these files belong to the Arduino core and should be available when installign through Platformio.

@zekageri

They are included just the platformIO can not open them for some reason. I use my own library folder inside my project. Every library is in that folder. And some of them are using these libs too without a problem. :/

Member Avatar

12 Years Ago

Hi,
please help me with error.
I want to run simple project below.
I have a lot of errors below and don’t know how to fix it.

I use GCC port (Mingw)

#include "mysql/my_global.h"
#include "mysql/mysql.h"
#include <stdio.h>

int main(int argc, char **argv)
{
  printf("MySQL client version: %sn", mysql_get_client_info());
}

error
———-Build Started———
C:WINDOWSsystem32cmd.exe /c «»mingw32-make.exe» -j 2 -f «learn MySQL_wsp.mk»»
———-Building project:[ basics — Debug ]———-
mingw32-make.exe[1]: Entering directory `C:/Documents and Settings/stefano/Desktop/C++/learn MySQL’
gcc -c «C:/Documents and Settings/stefano/Desktop/C++/learn MySQL/main.c» -g -DSTACK_DIRECTION=1 -o ./Debug/main.o «-I.» «-I.» «-I./mysql»
In file included from C:/Documents and Settings/stefano/Desktop/C++/learn MySQL/mysql/my_global.h:76,
from C:/Documents and Settings/stefano/Desktop/C++/learn MySQL/main.c:1:
./mysql/config-win.h:203:1: warning: «finite» redefined
./mysql/config-win.h:161:1: warning: this is the location of the previous definition
./mysql/config-win.h:245:1: warning: «STACK_DIRECTION» redefined
<command-line>: warning: this is the location of the previous definition
In file included from C:/Documents and Settings/stefano/Desktop/C++/learn MySQL/mysql/my_global.h:415,
from C:/Documents and Settings/stefano/Desktop/C++/learn MySQL/main.c:1:
c:mingw-4.4.1bin../lib/gcc/mingw32/4.4.1/../../../../include/stdio.h:491: error: expected declaration specifiers or ‘…’ before ‘(‘ token
c:mingw-4.4.1bin../lib/gcc/mingw32/4.4.1/../../../../include/stdio.h:491: error: expected declaration specifiers or ‘…’ before ‘(‘ token
In file included from C:/Documents and Settings/stefano/Desktop/C++/learn MySQL/main.c:1:
C:/Documents and Settings/stefano/Desktop/C++/learn MySQL/mysql/my_global.h:616:1: warning: «my_reinterpret_cast» redefined
In file included from C:/Documents and Settings/stefano/Desktop/C++/learn MySQL/mysql/my_global.h:76,
from C:/Documents and Settings/stefano/Desktop/C++/learn MySQL/main.c:1:
./mysql/config-win.h:307:1: warning: this is the location of the previous definition
In file included from C:/Documents and Settings/stefano/Desktop/C++/learn MySQL/main.c:1:
C:/Documents and Settings/stefano/Desktop/C++/learn MySQL/mysql/my_global.h:1566: error: static declaration of ‘rint’ follows non-static declaration
mingw32-make.exe[1]: *** [Debug/main.o] Error 1
mingw32-make.exe[1]: Leaving directory `C:/Documents and Settings/stefano/Desktop/C++/learn MySQL’
mingw32-make.exe: *** [All] Error 2
———-Build Ended———-
5 errors, 5 warnings

Edited

12 Years Ago
by Stefano Mtangoo because:

n/a

Понравилась статья? Поделить с друзьями:
  • Error expected asm or attribute before token
  • Error expected primary expression before long
  • Error expected after class definition
  • Error expected primary expression before int
  • Error expected primary expression before float