Error: expected constructor, destructor, or type conversion before & #39;. & #39; token, expectedconstructor

Source: Internet
Author: User

Error: expected constructor, destructor, or type conversion before '.' token, expectedconstructor

Today, I encountered the following error when writing code: expected constructor, destructor, or type conversion before '.' token. I checked it online. It turns out that I cannot perform full-local operations.It cannot be used for assignment, operation, and function call.Only variables can be declared and initialized.

The following is my error code:

# Include <iostream>

Int a [100];

Memset (a, 0, sizeof (a); // where an error occurs, you cannot assign values to variables in the full-local mode.

Int main (){

// Doing something

}

 

 

The differences between initialization, declaration, and definition cannot be clarified here: The following is a summary Article reposted by others.

Differences between C ++ statements and definitions

 

In section 2.3.5, version 4 of C ++ Primer:

① Variable definition: it is used to allocate storage space for the variable, and can also specify the initial value for the variable. In the program, variables have only one definition.

② Variable Declaration: used to indicate the type and name of the variable to the program.

③ Definition is also Declaration: when we define a variable, we declare its type and name.

④ Extern Keyword: declare the variable name without defining it by using the extern keyword.

 

1. The definition is also a declaration, and the extern Declaration is not a definition, that is, no storage space is allocated. Extern tells the compiler that the variable is defined elsewhere.

Example: extern int I; // declaration, not Definition

Int I; // Declaration, also defined

 

2. If the Declaration has an initialization type, it will be treated as a definition, even if the extern is added before. The object can be initialized only when the extern declaration is outside the function.

Example: extern double pi = 3.1416; // Definition

 

3. The differences between the Declaration and definition of a function are relatively simple. If {} is included, the function is defined. Otherwise, the function is declared.

Example: extern double max (double d1, double d2); // Declaration

 

4. Unless the extern keyword extern exists, all variables are defined.

Example: extern int I; // Declaration

Int I; // Definition

 

 

Programming style:

1. Do not put variable definitions in. H files, which may lead to repeated definition errors.

2. Use the static keyword whenever possible to restrict the variable definition to the scope of the source file, unless the variable is designed to be global.

3. You can declare a variable in the header file. When using this header file, the variable is declared.

 

Summary:

Variables must be defined or declared before they are used.

In a program, variables can be defined only once, but can be declared multiple times.

Define the bucket to be allocated without declaring it.

 

C ++ programs are usually composed of many files. To allow multiple files to access the same variables, C ++ distinguishes between declarations and definitions.

The definition of a variable is used to allocate storage space for the variable. You can also specify the initial value for the variable. In a program, variables have only one definition.

A declaration is used to indicate the type and name of a variable to a program. Definition is also Declaration: when defining a variable, we declare its type and name. You can declare a variable name without defining it by using extern. Declarations that do not define variables include the object name, object type, and the keyword extern before the object type.

The extern declaration is neither a definition nor a bucket. In fact, it only indicates that the variable is defined elsewhere in the program. Variables in the program can be declared multiple times, but can only be defined once.

Only when the Declaration is a definition can the Declaration have an initialization type, because only the definition can allocate storage space. The initialization type must have a bucket for initialization. If the Declaration has an initialization type, it can be considered as a definition, even if the declaration is marked as extern.

Any variables used in multiple files must be declared separate from definitions. In this case, a file contains the definition of the variable, and other files that use the variable contain the declaration (rather than definition) of the variable ).

 

-------------------------------------------------------------------- Definitions and declarations in header files

 

Note that variables cannot be defined in header files !!! Generally, only variables are declared in the header file, because the header file must be included by other files (that is, # include). If you put the definition in the header file, you cannot avoid defining variables multiple times, C ++ does not allow multiple definitions of variables. A program only defines the specified variables once and can be declared countless times.

However, there are three exceptions. The definition of the third entity can also be placed in the header file.

1. Definitions of known const variables at compilation can be placed in header files.

For example, const int num (10 );

2. class definitions can be placed in header files.

3. inline functions

These three entities can be defined in multiple source files, as long as the definitions in each source file are the same.

 

/---------------------------------------------------------------------- Definition and declaration in the header file

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.