生成CXX对象CMakeFiles错误:ProductCategory *结果= 0;未在范围中声明SaleItem的产品类别指针为成员

问题描述

我有一个SaleItem类,该类具有另一个类Product Category的指针作为成员函数,SaleItem类的getter返回指向ProductCategory的指针导致了错误。 IDE不会直接给出错误,而是在我构建C ++代码库时给出的。 SWIG界面会在构建过程中为我解释c ++代码。完成后,我们将使用SWIG将C ++连接到Perl,在该特定部分中将引发错误

  1. 代码的第一段摘录来自SALEITEM类
  2. 第二个是描述错误的命令行文本
  3. 第三部分是产品类别分类摘要
  4. 第四,最后是SWIG解释。

此问题底部代码部分是终端在哪里引用错误。但我相信这是在我的C ++代码中。我已尽我所知来解决此问题。因此寻求您的帮助

/**
 * @brief returns the saleItem's Product Category Details in a pointer
 * @return ProductCategory*
 *
 */

ProductCategory* SaleItem::getProductCategory(){
    return m_productCategory.get();
}


private:
        /**
         * @brief load SaleItem object from pc_sale_item table.
         * 
         * @param SaleItemId
         * 
         * @return true
         * @return false
         * 
         */
        bool _load(const string& pId);
    private:
        std::vector<shared_ptr<SaleItemCommission>> vCommissions;
        unique_ptr<Contract> pRetailerContract;
        unique_ptr<Contract> pServiceContract;
        std::vector<std::string> _vTags;
        unique_ptr<ProductCategory> m_productCategory;
        std::string m_groupNum;
    };
}


上面是SaleItem类,错误抛出在函数getProductCategory()




/home/rrajan/public_html/cms/src/cms_xl/build/swig/CMakeFiles/sale.dir/salePERL_wrap.cxx: In function ‘void _wrap_SaleItem_getProductCategory(PerlInterpreter*,CV*)’:
/home/rrajan/public_html/cms/src/cms_xl/build/swig/CMakeFiles/sale.dir/salePERL_wrap.cxx:23133:5: error: ‘ProductCategory’ was not declared in this scope
     ProductCategory *result = 0 ;
     ^~~~~~~~~~~~~~~
/home/rrajan/public_html/cms/src/cms_xl/build/swig/CMakeFiles/sale.dir/salePERL_wrap.cxx:23133:5: note: suggested alternative:
In file included from /home/rrajan/public_html/cms/src/cms_xl/swig/../src/../src/sale/SaleItem.h:19:0,from /home/rrajan/public_html/cms/src/cms_xl/build/swig/CMakeFiles/sale.dir/salePERL_wrap.cxx:2119:
/home/rrajan/public_html/cms/src/cms_xl/swig/../src/../src/sale/../core/ProductCategory.h:33:8: note:   ‘Cms::ProductCategory’
  class ProductCategory: public Virtual::Base
        ^~~~~~~~~~~~~~~
/home/rrajan/public_html/cms/src/cms_xl/build/swig/CMakeFiles/sale.dir/salePERL_wrap.cxx:23133:22: error: ‘result’ was not declared in this scope
     ProductCategory *result = 0 ;
                      ^~~~~~
/home/rrajan/public_html/cms/src/cms_xl/build/swig/CMakeFiles/sale.dir/salePERL_wrap.cxx:23133:22: note: suggested alternative: ‘res1’
     ProductCategory *result = 0 ;
                      ^~~~~~
                      res1
/home/rrajan/public_html/cms/src/cms_xl/build/swig/CMakeFiles/sale.dir/salePERL_wrap.cxx:23146:36: error: expected primary-expression before ‘)’ token
         result = (ProductCategory *)(arg1)->getProductCategory();
                                    ^
make[2]: *** [CMakeFiles/sale.dir/build.make:83: CMakeFiles/sale.dir/CMakeFiles/sale.dir/salePERL_wrap.cxx.o] Error 1



上面是抛出的命令行错误 以下是产品类别类别

class ProductCategory: public Virtual::Base
    {
        public:
        /**
         * @brief Construct a new Product Category object
         *
         * @param sProductId
         */
        ProductCategory(){}

        /**
         * @brief Construct a new Product Category object
         * @param sProductId
         */
        ProductCategory(string productID);

        /**
         * @brief Destructs a Product Category object
         *
         */
        ~ProductCategory(){}

SALEITEM :: getProductcategory()的SWIG解释为以下代码


XS(_wrap_SaleItem_getProductCategory) {
  {
    Cms::SaleItem *arg1 = (Cms::SaleItem *) 0 ;
    void *argp1 = 0 ;
    int res1 = 0 ;
    int argvi = 0;
    ProductCategory *result = 0 ;
    dXSARGS;
    
    if ((items < 1) || (items > 1)) {
      SWIG_croak("Usage: SaleItem_getProductCategory(self);");
    }
    res1 = SWIG_ConvertPtr(ST(0),&argp1,SWIGTYPE_p_Cms__SaleItem,0 |  0 );
    if (!SWIG_IsOK(res1)) {
      SWIG_exception_fail(SWIG_ArgError(res1),"in method '" "SaleItem_getProductCategory" "',argument " "1"" of type '" "Cms::SaleItem *""'"); 
    }
    arg1 = reinterpret_cast< Cms::SaleItem * >(argp1);
    {
      try {
        result = (ProductCategory *)(arg1)->getProductCategory();
      }
      catch (sql::sqlException &) {
        croak("sql exception");
      }
    }
    ST(argvi) = SWIG_NewPointerObj(SWIG_as_voidptr(result),SWIGTYPE_p_ProductCategory,0 | 0); argvi++ ;
    
    XSRETURN(argvi);
  fail:
    
    SWIG_croak_null();
  }


解决方法

所以最后,我找到了答案... 由于某种原因,尽管位于名为Cms的命名空间中的ProductCategory类定义(也与包含ProductCategory类的对象实例的SaleItem类定义的相同命名空间)仍不在范围内。

SaleItem类负责引发该错误,指出其类中的ProductCategory实例不在SaleItem的范围内。

为解决此错误,我发现,如果我将Cms :: ProductCategory添加到SaleItem类的getter方法中,则SaleItem类会立即意识到ProductCategory对象在同一范围内。

   /**
    * @brief returns the saleItem's Product Category Details in a pointer
    * @return ProductCategory*
    *
    */

    Cms::ProductCategory* SaleItem::getProductCategory(){
      return m_productCategory.get();
    }

添加 Cms :: 解决了该错误。

我发现的另一件事是,同一SaleItem类中还有其他Object Instance,例如Contract类Object;但是,在SaleItem类中声明了Contract Object的实例getter时,尽管没有在其后添加 Cms ::

/**
     * @brief returns a pointer to the associated contract object
     * this function make sures that returned pointer is not NULL
     * saves the system from crashing
     * @return Contract*
     *
     */
    Contract* getContract();

然后,我在Contract类中找到了一些我不理解的声明,但是我认为它们确保Contract属于SaleItem的范围。因此,因此Contract对象不会引发与ProductCategory相同的错误。

下面是这些声明行。

 namespace Cms
    {
       class Sale;
       class SaleItem;

       /**
        * @brief A contract class. Which has contract related information.
        * This is mainly used as an interface for service contract and retailer contract.
        * It is derived from Base.
        *
        */
        class Contract : public Virtual::Base {
        }
    }

我希望这对遇到此问题的人有用。也许其他人可以对此做出更多解释。