1

私の問題は、別のクラスへの参照によってオブジェクトを送信できないように見えることです。これをネットで調べたのは運が悪かった。私の情報源を調べて、何かアイデアがあれば教えてください。TYIA - ローランド

これらのエラーも発生します

 error: field 'PgmClass' has incomplete type
 error: 'PgmClass' does not name a type
 error: expected ')' before 'thesource'
 error: 'm_hereitis' was not declared in this scope

#include <iostream>
#include "pgmclass.h"
#include "inclobj.h"

int main()
{

    char catchcin[256];

    PgmClass wilko;

    wilko.addToSet( 7 );
    wilko.addToSet( 8 );
    wilko.addToSet( 9 );

    InclObj alpha( wilko );

    wilko.addToSet( 10 );
    wilko.addToSet( 11 );

    // This doesn't work
    alpha.eraseOne( 10 );

    // How can I get this to work using referances?

    std::cout << "Program Running." << std::endl;
    std::cin >> catchcin;

    return 0;
}


----------

#include <set>

class PgmClass  {

      public:
    int addToSet( int );
    bool eraseSet( int );
    std::set<int> m_userset;
};

int PgmClass::addToSet( int theint )    {

    m_userset.insert( theint );
}

bool PgmClass::eraseSet( int eraseint )  {

    m_userset.erase( eraseint );
}


----------

class InclObj   {

      public:
    InclObj( PgmClass );
    void eraseOne( int );

    PgmClass m_hereitis;
};

InclObj::InclObj( PgmClass thesource )    {

    m_hereitis = thesource;
}

void InclObj::eraseOne( int findint )    {

    m_hereitis.eraseSet( findint );
}
4

1 に答える 1

1

You need to place your main at the end of the file. (Normaly you add the class in a separate .h file -with the implementation in another .cpp file-, with you include before use the class). Define, the member as reference:

class InclObj
{

      public:
            InclObj( PgmClass& );
            void eraseOne( int );

            PgmClass& m_hereitis;
};

InclObj::InclObj( PgmClass& thesource ) :  m_hereitis (thesource)
{

}

Doing this you assume some responsibilities. For example, don’t use eraseOne() after the original object has been deleted. Don’t try to add a function like InclObj::use_now_this_other_object(PgmClass& other_source), etc. But I assumed you know about…</p>

于 2013-01-17T13:42:55.497 に答える