0

I am trying to upload a solution to an OJ ,the judge uses GCC I have received the following errors and having no idea about them.

error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘{’ token
void insert(int in){
^

In function ‘main’:
error: ‘struct mymultiset_int’ has no member named ‘insert’
x.insert(t);
^

error: ‘struct mymultiset_int’ has no member named ‘getmax’
printf("%d\n",x.getmax());
^
error: ‘struct mymultiset_int’ has no member named ‘_delete’
x._delete(0);
^

My code looks like this:

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int t;
#define swap(a,b) t=b,b=a,a=t
/*
when using swap, I use format like swap(x,y); or swap(x,y),
*/

struct mymultiset_int{
    int e[100000],end;
    void insert(int in){...}
    int getmax(){ return e[0]; }
    void _delete(int i){...}
}x;
int main(){
    x.end=0;memset(x.e,0,sizeof(x.e));
    int N,t;scanf("%d",&N);
    char i[2];
    while (N--){
        scanf("%s",i);
        if (i[0]=='A'){
            scanf("%d",&t);
            x.insert(t);
        }
        else{
            printf("%d\n",x.getmax());
            x._delete(0);
        }
    }
}
8
  • 5
    You can't use functions in a struct in C Commented May 8, 2015 at 14:24
  • 1
    Is C not C++. Struct is not like a Class... Commented May 8, 2015 at 14:27
  • 1
    @Brian, not necessarily. The given macro definition is a valid C expression. It would be safer to put it in parentheses, though: (t=b,b=a,a=t). Commented May 8, 2015 at 14:27
  • 1
    @Brian yes, there is a difference. Using the comma operator it's all one expression (whose type and value are those of the sub-expression following the last comma). You can use it anywhere you can use a value. With semicolons it's three separate statements. Commented May 8, 2015 at 14:31
  • 1
    Moral of this story: do not use a C++ compiler as your reference compiler for C development. C++ and C are different languages. Commented May 8, 2015 at 14:37

1 Answer 1

1

you cannot define function in structure in C.

But it is possible in c++.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.