array of structures initialization

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve

    #1

    array of structures initialization

    A coworker just returned to C++ coding after a long hiatus and asked me how
    to accomplish this task. What seemed liked simple issue, we have been unable
    to resolve. He has declared a structure that includes an MFC class and wants
    to initialize it at declaration time. Something like this:

    In the h. file:

    namespace SomeNamespace
    {

    struct SomeStruct
    {
    long theNumber;
    CPoint thePoint;
    };

    SomeStruct arrssTheArray[] =
    {
    {
    1,
    CPoint(1,1)
    },
    {
    2,
    CPoint(2,2)
    }
    };
    };

    I have about 5 years experience in C\C++ but am self-taught so I can't rely
    on any training background and I have been unable to find a similar example
    of code. The compiler compliains that is cannot cast the long to a
    "SomeStruct ".

    We also tried:

    In the .h file:

    namespace SomeNamespace
    {

    struct SomeStruct
    {
    long theNumber;
    CPoint thePoint;
    };

    SomeStruct arrssTheArray[];
    };

    In the .cpp file:

    #include "SomeFile.h "

    SomeNamespace:: SomeStruct SomeNamespace:: arrssTheArray[] =
    {
    {
    1,
    CPoint(1,1)
    },
    {
    2,
    CPoint(2,2)
    }
    };

    In this case the compiler complains about a redefinition. Can this task even
    be done? Where can one go for information of this sort? I have about ~4
    books on C\C++ but cannot seem to find a definitive answer for issues like
    this. Thanks all.

    Steve


  • Rob Williscroft

    #2
    Re: array of structures initialization

    Steve wrote in news:I7zr9q.Inr @news.boeing.co m in comp.lang.c++:
    [color=blue]
    > A coworker just returned to C++ coding after a long hiatus and asked
    > me how to accomplish this task. What seemed liked simple issue, we
    > have been unable to resolve. He has declared a structure that includes
    > an MFC class and wants to initialize it at declaration time. Something
    > like this:
    >[/color]

    #include <iostream>
    #include <utility>

    /** Simple hack to post *compilable* code
    */
    typedef std::pair< int, int > CPoint;

    namespace SomeNamespace
    {
    struct SomeStruct
    {
    long theNumber;
    CPoint thePoint;
    };

    SomeStruct a[] =
    {
    { 1, CPoint(1,1) },
    { 2, CPoint(2,2) }
    };

    /** Needs to extern otherwise its a *defenition*
    */
    extern SomeStruct b[];
    }

    /** NOT a redefenition as the above is only a declaration
    */
    SomeNamespace:: SomeStruct SomeNamespace:: b[] =
    {
    { 1, CPoint(1,1) },
    { 2, CPoint(2,2) }
    };

    int main()
    {
    std::cout << SomeNamespace:: a[1].thePoint.first << '\n';
    std::cout << SomeNamespace:: b[1].thePoint.first << '\n';
    }

    The above compiles fine with every compiler I tried, including
    MSVC 7.1.

    HTH.

    Rob.
    --

    Comment

    • Steve

      #3
      Re: array of structures initialization

      Thank you Rob, but no dice... exact code I used:

      namespace Something
      {
      typedef std::pair< int, int > CPoint;

      struct SomeStruct
      {
      long theNumber;
      CPoint thePoint;
      };

      extern SomeStruct b[];
      }

      Something::Some Struct Something::b[] =
      {
      { 1, CPoint(1,1) },
      { 2, CPoint(2,2) }
      };

      Produces the following error using VC6.

      error C2440: 'initializing' : cannot convert from 'const int' to 'struct
      JSOWCT::SomeStr uct'
      No constructor could take the source type, or constructor overload
      resolution was ambiguous

      ????
      Thanks again.
      Steve


      "Rob Williscroft" <rtw@freenet.co .uk> wrote in message
      news:Xns95B1884 9C58E6ukcoREMOV Efreenetrtw@130 .133.1.4...[color=blue]
      > Steve wrote in news:I7zr9q.Inr @news.boeing.co m in comp.lang.c++:
      >[color=green]
      > > A coworker just returned to C++ coding after a long hiatus and asked
      > > me how to accomplish this task. What seemed liked simple issue, we
      > > have been unable to resolve. He has declared a structure that includes
      > > an MFC class and wants to initialize it at declaration time. Something
      > > like this:
      > >[/color]
      >
      > #include <iostream>
      > #include <utility>
      >
      > /** Simple hack to post *compilable* code
      > */
      > typedef std::pair< int, int > CPoint;
      >
      > namespace SomeNamespace
      > {
      > struct SomeStruct
      > {
      > long theNumber;
      > CPoint thePoint;
      > };
      >
      > SomeStruct a[] =
      > {
      > { 1, CPoint(1,1) },
      > { 2, CPoint(2,2) }
      > };
      >
      > /** Needs to extern otherwise its a *defenition*
      > */
      > extern SomeStruct b[];
      > }
      >
      > /** NOT a redefenition as the above is only a declaration
      > */
      > SomeNamespace:: SomeStruct SomeNamespace:: b[] =
      > {
      > { 1, CPoint(1,1) },
      > { 2, CPoint(2,2) }
      > };
      >
      > int main()
      > {
      > std::cout << SomeNamespace:: a[1].thePoint.first << '\n';
      > std::cout << SomeNamespace:: b[1].thePoint.first << '\n';
      > }
      >
      > The above compiles fine with every compiler I tried, including
      > MSVC 7.1.
      >
      > HTH.
      >
      > Rob.
      > --
      > http://www.victim-prime.dsl.pipex.com/[/color]


      Comment

      • Victor Bazarov

        #4
        Re: array of structures initialization

        Steve wrote:[color=blue]
        > Thank you Rob, but no dice... exact code I used:
        >
        > namespace Something
        > {
        > typedef std::pair< int, int > CPoint;
        >
        > struct SomeStruct
        > {
        > long theNumber;
        > CPoint thePoint;
        > };
        >
        > extern SomeStruct b[];
        > }
        >
        > Something::Some Struct Something::b[] =
        > {
        > { 1, CPoint(1,1) },[/color]

        Try adding a constructor to your SomeStruct:

        struct SomeStruct
        {
        long theNumber;
        CPoint thePoint;
        SomeStruct(long N, int i1, int i2)
        : theNumber(N), thePoint(std::m ake_pair(i1,i2) ) {}
        };

        and initialising it like this:

        SomeStruct(1, 1, 1)

        [color=blue]
        > { 2, CPoint(2,2) }
        > };
        >
        > Produces the following error using VC6.
        >
        > error C2440: 'initializing' : cannot convert from 'const int' to 'struct
        > JSOWCT::SomeStr uct'
        > No constructor could take the source type, or constructor overload
        > resolution was ambiguous
        >
        > ????[/color]

        And upgrade your compiler to v7.1 ASAP.
        [color=blue]
        > Thanks again.
        > Steve
        > [...][/color]

        And don't top-post.

        V

        Comment

        Working...