Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expected initializer before function name

#include <iostream>
#include <string>

using namespace std;

struct sotrudnik {
    string name;
    string speciality;
    string razread;
    int zarplata;
}

sotrudnik create(string n,string spec,string raz,int sal) {
    sotrudnik temp;
    temp.name=n;
    temp.speciality=spec;
    temp.razread=raz;
    temp.zarplata=sal;
    return temp;
}
*sotrudnik str_compare (string str1, string str2, sotrudnik sot1, sotrudnik sot2)

I try to learn C++. But when i try to compile this code with GCC-4.4.5 by using the options " g++ -Wall -c ", I get the following error:

g++ -Wall -c "lab2.cc" (in directory: /home/ion/Univer/Cpp)

lab2.cc:11: error: expected initializer before create
lab2.cc:20: error: expected constructor, destructor, or type conversion before str_compare
Compilation failed.

Both errors are tied to the function declarations. (round 11 is the declaration of function create, round 20 - of the function str_compare). Tried to google for these kinds of errors, but couldn't find examples of similar errors, as the error messages are very generic. How can I understand their meaning and how to solve them? Thank you very much for your attention.

like image 243
Ion Avatar asked Apr 15 '11 10:04

Ion


People also ask

What is expected initializer before?

"Expected initializer before '. ' token" just means that it found code that didn't make sense to it - the "." character isn't legal there. Where the line is declaring a variable with a "." in the name, which is illegal because "." is an operator, and you can't declare a "multipart" variabel like that in C.

What is expected initializer before INT?

There is nothing before int main as all functions are declare and defined in different files.

What is initializer in Arduino?

The member initializer list is specified after the arguments of the constructor. It begins with a colon followed by the name of each variable to initialize, separated by commas [1]. The value to assign to each data member is specified between parenthesis after the name of each data member of the list.


1 Answers

You are missing a semicolon at the end of your 'struct' definition.

Also,

*sotrudnik

needs to be

sotrudnik*
like image 188
bmargulies Avatar answered Sep 24 '22 14:09

bmargulies