我的问题是,我似乎无法通过引用另一个对象来发送对象。我很少在网上查到这个。如果可以的话,请检查我的消息来源,并告诉我您是否有任何想法。 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 );
}

最佳答案

您需要将主目录放置在文件末尾。 (通常,将类添加到单独的.h文件中-将实现添加到另一个.cpp文件中-在使用该类之前将其包括在内)。
定义,该成员作为参考:

class InclObj
{

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

            PgmClass& m_hereitis;
};

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

}

这样做会承担一些责任。例如,在删除原始对象后,请勿使用eraseOne()。不要尝试添加InclObj::use_now_this_other_object(PgmClass& other_source)之类的函数,但是我想您知道……

08-04 17:38