小编use*_*611的帖子

C++从函数返回值

我试图从函数返回一个值,但它返回的值是值1.

它假设有5个参数computeCivIndex(),即使我硬编码了值,我收到的输出仍然是1.

为什么会这样?

float LocationData::computeCivIndex()
{
     civNum1 = 45.0 / 100;
     civNum2 = 20 + 50;
     civNum3 = civNum2 / 200;
     civNum4 = civNum1 - civNum3;
     civNum5 = 5 + 10;


    return civNum;
}

//display data
void LocationData::displaydata()
{
cout << "CIV value: " << computeCivIndex << endl;
}
Run Code Online (Sandbox Code Playgroud)

c++ function return-value

0
推荐指数
1
解决办法
168
查看次数

编译错误:: vector :: iteratator

每当我编译我的prg时,我都会遇到这个错误.

c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h:5394:5: note: template<class _RAIter> void std::sort(_RAIter, _RAIter)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h:5430:5: note: void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<PointTwoD*, std::vector<PointTwoD> >, _Compare = bool (MissionPlan::*)(PointTwoD&, PointTwoD&)]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h:5430:5: note:   no known conversion for argument 3 from '<unresolved overloaded function type>' to 'bool (MissionPlan::*)(PointTwoD&, PointTwoD&)'
Run Code Online (Sandbox Code Playgroud)

下面实际是我的.cpp文件..

bool MissionPlan::sortByCiv(const PointTwoD &t1, const PointTwoD &t2)
{
    return t1.locationdata.getCivIndex() < t2.locationdata.getCivIndex();
}

void MissionPlan::topfives()
{   

    topfive.assign( point1.begin(), point1.end() ); 

    sort(topfive.begin(), topfive.end(), sortByCiv);

    for(int i=0; i < 5; i++)
    {
        topfive.at(i).displayPointdata();
    }

}
Run Code Online (Sandbox Code Playgroud)

missionplan.h

class MissionPlan
{ …
Run Code Online (Sandbox Code Playgroud)

c++ sorting compilation

-2
推荐指数
1
解决办法
262
查看次数

c ++虚函数总是必须是const?

我只是想知道所有虚函数是否必须是const?

我遇到了一些问题,因为当我打算将它们打印出来时,该区域总是为我的方块返回0.如果有人能够启发我,我将不胜感激.

shapetwod.h

class ShapeTwoD
{
    protected:
        string name, warpSpace;
        bool containsWarpSpace;
    public:
        //constructor
        ShapeTwoD();
        ShapeTwoD(string, bool);

        //accessors/set function
        void setName(string);

        //mutator/get function
        string getName();



        //methods
        virtual double computeArea();
        virtual void view();
};
Run Code Online (Sandbox Code Playgroud)

shapetwod.cpp

ShapeTwoD::ShapeTwoD()
{
    string name = "";
}

ShapeTwoD::ShapeTwoD(string ShapeName)
{
    name = ShapeName;
}

void ShapeTwoD::setName(string shapeName)
{
    name=shapeName;
}

string ShapeTwoD::getName()
{
    return name;
}

double ShapeTwoD::computeArea()
{
    return 0;
}

void ShapeTwoD::view()
{
    cout << "Area is: " << endl;
}
Run Code Online (Sandbox Code Playgroud)

square.h

class Square:public …
Run Code Online (Sandbox Code Playgroud)

c++ virtual const

-6
推荐指数
1
解决办法
338
查看次数

标签 统计

c++ ×3

compilation ×1

const ×1

function ×1

return-value ×1

sorting ×1

virtual ×1