Structures

Dear Sir,
Placed below is a part of a small program on structures. The structure < MyString >
defines an array . and a pointer.

The comilaer is giveing three errors , the first one is that C++ forbids declaration without a data type . Please refer ther comiler log - 1.
Although both the elements have been defined within the struct < MyString > . This anomoly is not understanable.

Secondly , when datatype MyStructure was placed in front on line 23 , the the compiler indicates that declaration already made at line 21 .

Could you please explain the anomoly and provide the remedy.
Thanking you.
/* 786 */
/* ***** Structures ***** */


#include
#include
#include

using namespace std ;


struct MyStructure
{
char MyString [40] ; // String of max 40 chracters
int MyInteger ; // const variable

} ;
MyStructure StructureVariable [20] ; // An array of 20 elements
MyStructure * StructurePointer ; // a pointer --- Line 21

StructurePointer = StructureVariable ; // Line - 23


int main ()
{
cout<<endl<<endl ;
cout<<endl<<endl ;
cout <<" " ;

cout << " My String ! : << endl<<endl ;
system ("pause") ;
return 0 ;
}




---------------------------------------------------------------------
/* COMPILE LOG - 1 */

Compiler: Default compiler
Executing g++.exe...
g++.exe "F:Dev-CppStructures.cpp" -o "F:Dev-CppStructures.exe" -I"F:Dev-Cppincludec++3.3.1ackward" -I"F:Dev-Cppincludec++3.3.1mingw32" -I"F:Dev-Cppincludec++3.3.1" -I"F:Dev-Cppinclude" -L"F:Dev-Cpplib"
F:/Dev-Cpp/Structures.cpp:23: error: ISO C++ forbids declaration of `
StructurePointer' with no type
F:/Dev-Cpp/Structures.cpp:23: error: conflicting types for `int
StructurePointer'
F:/Dev-Cpp/Structures.cpp:21: error: previous declaration as `
MyStructure*StructurePointer'

Execution terminated

-------------------------------------------------------------------
/* COMPILE LOG - 2 */


Compiler: Default compiler
Executing g++.exe...
g++.exe "F:Dev-CppStructures.cpp" -o "F:Dev-CppStructures.exe" -I"F:Dev-Cppincludec++3.3.1ackward" -I"F:Dev-Cppincludec++3.3.1mingw32" -I"F:Dev-Cppincludec++3.3.1" -I"F:Dev-Cppinclude" -L"F:Dev-Cpplib"
F:/Dev-Cpp/Structures.cpp:23: error: conflicting types for `MyStructure
StructurePointer'

F:/Dev-Cpp/Structures.cpp:21: error: previous declaration as `

MyStructure*StructurePointer'

Execution terminated



Comments

  • : Dear Sir,
    : Placed below is a part of a small program on structures. The structure < MyString >
    : defines an array . and a pointer.
    :
    : The comilaer is giveing three errors , the first one is that C++ forbids declaration without a data type . Please refer ther comiler log - 1.
    : Although both the elements have been defined within the struct < MyString > . This anomoly is not understanable.
    :
    : Secondly , when datatype MyStructure was placed in front on line 23 , the the compiler indicates that declaration already made at line 21 .
    :
    : Could you please explain the anomoly and provide the remedy.
    : Thanking you.

    [blue]Please use code tags when posting code samples, for example:[/blue]

    [code]
    /* 786 */
    /* ***** Structures ***** */

    #include
    #include
    #include

    using namespace std ;


    struct MyStructure
    {
    char MyString [40] ; // String of max 40 chracters
    int MyInteger ; // const variable
    } ;

    MyStructure StructureVariable [20] ; // An array of 20 elements
    MyStructure * StructurePointer ; // a pointer --- Line 21

    [red]StructurePointer = StructureVariable ; // Line - 23 [/red]


    int main ()
    {
    cout<<endl<<endl ;
    cout<<endl<<endl ;
    cout <<" " ;

    cout << " My String ! : << endl<<endl ;
    system ("pause") ;
    return 0 ;
    }
    [/code]

    [blue]
    The line in [red]red[/red] above isn't legal, you can have a declaration statment of a variable and initialize it as a single code statement, but you can't have a seperate initialization step outside of a block of code in a function.

    Illegal:
    [code]MyStructure * StructurePointer ;

    [red]// Can't have a seperate initialization statement outside of a block of code in some function
    StructurePointer = StructureVariable ;[/red][/code]

    Legal:
    [code]// This is legal since it is a single combined declaration/initialization statement
    MyStructure * StructurePointer = StructureVariable ;[/code]

    Other than that, the [italic]stdlib.h[/italic] header should be replaced with [italic]cstdlib[/italic] instead.[/blue]



    :
    : ---------------------------------------------------------------------
    : /* COMPILE LOG - 1 */
    :
    : Compiler: Default compiler
    : Executing g++.exe...
    : g++.exe "F:Dev-CppStructures.cpp" -o "F:Dev-CppStructures.exe" -I"F:Dev-Cppincludec++3.3.1ackward" -I"F:Dev-Cppincludec++3.3.1mingw32" -I"F:Dev-Cppincludec++3.3.1" -I"F:Dev-Cppinclude" -L"F:Dev-Cpplib"
    : F:/Dev-Cpp/Structures.cpp:23: error: ISO C++ forbids declaration of `
    : StructurePointer' with no type
    : F:/Dev-Cpp/Structures.cpp:23: error: conflicting types for `int
    : StructurePointer'
    : F:/Dev-Cpp/Structures.cpp:21: error: previous declaration as `
    : MyStructure*StructurePointer'
    :
    : Execution terminated
    :
    : -------------------------------------------------------------------
    : /* COMPILE LOG - 2 */
    :
    :
    : Compiler: Default compiler
    : Executing g++.exe...
    : g++.exe "F:Dev-CppStructures.cpp" -o "F:Dev-CppStructures.exe" -I"F:Dev-Cppincludec++3.3.1ackward" -I"F:Dev-Cppincludec++3.3.1mingw32" -I"F:Dev-Cppincludec++3.3.1" -I"F:Dev-Cppinclude" -L"F:Dev-Cpplib"
    : F:/Dev-Cpp/Structures.cpp:23: error: conflicting types for `MyStructure
    : StructurePointer'
    :
    : F:/Dev-Cpp/Structures.cpp:21: error: previous declaration as `
    :
    : MyStructure*StructurePointer'
    :
    : Execution terminated
    :
    :
    :
    :

  • : : Dear Sir,
    : : Placed below is a part of a small program on structures. The structure < MyString >
    : : defines an array . and a pointer.
    : :
    : : The comilaer is giveing three errors , the first one is that C++ forbids declaration without a data type . Please refer ther comiler log - 1.
    : : Although both the elements have been defined within the struct < MyString > . This anomoly is not understanable.
    : :
    : : Secondly , when datatype MyStructure was placed in front on line 23 , the the compiler indicates that declaration already made at line 21 .
    : :
    : : Could you please explain the anomoly and provide the remedy.
    : : Thanking you.
    :
    : [blue]Please use code tags when posting code samples, for example:[/blue]
    :
    : [code]
    : /* 786 */
    : /* ***** Structures ***** */
    :
    : #include
    : #include
    : #include
    :
    : using namespace std ;
    :
    :
    : struct MyStructure
    : {
    : char MyString [40] ; // String of max 40 chracters
    : int MyInteger ; // const variable
    : } ;
    :
    : MyStructure StructureVariable [20] ; // An array of 20 elements
    : MyStructure * StructurePointer ; // a pointer --- Line 21
    :
    : [red]StructurePointer = StructureVariable ; // Line - 23 [/red]
    :
    :
    : int main ()
    : {
    : cout<<endl<<endl ;
    : cout<<endl<<endl ;
    : cout <<" " ;
    :
    : cout << " My String ! : << endl<<endl ;
    : system ("pause") ;
    : return 0 ;
    : }
    : [/code]
    :
    : [blue]
    : The line in [red]red[/red] above isn't legal, you can have a declaration statment of a variable and initialize it as a single code statement, but you can't have a seperate initialization step outside of a block of code in a function.
    :
    : Illegal:
    : [code]MyStructure * StructurePointer ;
    :
    : [red]// Can't have a seperate initialization statement outside of a block of code in some function
    : StructurePointer = StructureVariable ;[/red][/code]
    :
    : Legal:
    : [code]// This is legal since it is a single combined declaration/initialization statement
    : MyStructure * StructurePointer = StructureVariable ;[/code]
    :
    : Other than that, the [italic]stdlib.h[/italic] header should be replaced with [italic]cstdlib[/italic] instead.[/blue]
    :
    :

    [code][green]
    Dear Sir,
    I am thankful for the reply ,
    I implied it , and the first error was rectified.
    Now the excution ( rather compilation ) is generating dual declaration error , as mentioned in Compiler log - 2 below. I could not find the method to remove this fault. Plz help.
    Thanks, [/green ] [/code]

    .......................................................................











    :
    : :
    : : ---------------------------------------------------------------------
    : : /* COMPILE LOG - 1 */
    : :
    : : Compiler: Default compiler
    : : Executing g++.exe...
    : : g++.exe "F:Dev-CppStructures.cpp" -o "F:Dev-CppStructures.exe" -I"F:Dev-Cppincludec++3.3.1ackward" -I"F:Dev-Cppincludec++3.3.1mingw32" -I"F:Dev-Cppincludec++3.3.1" -I"F:Dev-Cppinclude" -L"F:Dev-Cpplib"
    : : F:/Dev-Cpp/Structures.cpp:23: error: ISO C++ forbids declaration of `
    : : StructurePointer' with no type
    : : F:/Dev-Cpp/Structures.cpp:23: error: conflicting types for `int
    : : StructurePointer'
    : : F:/Dev-Cpp/Structures.cpp:21: error: previous declaration as `
    : : MyStructure*StructurePointer'
    : :
    : : Execution terminated
    : :
    : : -------------------------------------------------------------------
    : : /* COMPILE LOG - 2 */
    : :
    : :
    : : Compiler: Default compiler
    : : Executing g++.exe...
    : : g++.exe "F:Dev-CppStructures.cpp" -o "F:Dev-CppStructures.exe" -I"F:Dev-Cppincludec++3.3.1ackward" -I"F:Dev-Cppincludec++3.3.1mingw32" -I"F:Dev-Cppincludec++3.3.1" -I"F:Dev-Cppinclude" -L"F:Dev-Cpplib"
    : : F:/Dev-Cpp/Structures.cpp:23: error: conflicting types for `MyStructure
    : : StructurePointer'
    : :
    : : F:/Dev-Cpp/Structures.cpp:21: error: previous declaration as `
    : :
    : : MyStructure*StructurePointer'
    : :
    : : Execution terminated
    : :
    : :
    : :
    : :
    :
    :

  • I don't get any errors when compiling the code in Dev.
    Post the modified code.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories