小编Vis*_*kar的帖子

如何检查类中已定义的枚举类型变量是否已分配?

我想检查一个类中的变量是否已设置?我该怎么做?

enum Color {
   red,
   blue
};
class example {
   Color c;
   void set_color(Color c) { this->c = c; }
   Color get_color() { return c; }
   bool check_color_is_set() {
       if (c == Color) {} // compile error
       if (c == NULL) {} // compile error
       return true;
   }
Run Code Online (Sandbox Code Playgroud)

我想知道变量是否Color c已分配?

c++ enums

8
推荐指数
1
解决办法
150
查看次数

功能中的注释参数

我本来打算通过实施seekpos流缓冲的GNU在线文档源.我无法理解为什么__mode在行中注释ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out以及它为什么不抛出错误.

  virtual pos_type 
  seekpos(pos_type, ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out)
   {
      return pos_type(off_type(-1));
   }
Run Code Online (Sandbox Code Playgroud)

我可以理解评论的用法,如果它是以下格式:

void foo( pos_type, int /*blah*/ ){
...
}
Run Code Online (Sandbox Code Playgroud)

但是,在前一种情况下,还有意图分配一些东西__mode,因此我很惊讶没有在那里得到任何错误.

这是允许的吗?如果是,那为什么呢?

c++

4
推荐指数
1
解决办法
128
查看次数

尝试包含微型 obj 加载器头文件时出现 C++ 链接器错误

我正在尝试使用tiny_obj_loader将其加载.obj到我的程序中。我有以下使用该库的代码:

void LoadModel(vector<Shape *> &scene, const char *path) {
  attrib_t attrib;
  vector<shape_t> shapes;
  vector<material_t> materials;
  std::string error;
  bool ret = tinyobj::LoadObj(
    &attrib,
    &shapes,
    &materials,
    &error,
    path
  );
  ... // Do stuff with the returned data
Run Code Online (Sandbox Code Playgroud)

然而,这给了我以下链接器错误:

Build/skeleton.o: In function `LoadModel(std::vector<Shape*, 
std::allocator<Shape*> >&, char const*)':
skeleton.cpp:(.text+0x3025): undefined reference to `tinyobj::LoadObj(tinyobj::attrib_t*, std::vector<tinyobj::shape_t, std::allocator<tinyobj::shape_t> >*, std::vector<tinyobj::material_t, std::allocator<tinyobj::material_t> >*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*, char const*, char const*, bool)'
Makefile:43: recipe for target 'Build' failed
make: *** [Build] Error 1
Run Code Online (Sandbox Code Playgroud)

其中函数定义是: …

c++

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

未定义的引用cv :: imread(cv :: String const&,int)

我试图使用OpenCV编写一个简单的代码,但我无法成功编译它.以下是代码:

#include <iostream>
#include <stdio.h>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

using namespace std;
using namespace cv;

int main()
{
    Mat img = imread("insignia.jpg", -1);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它不会起作用.以下是我收到的错误消息.

/tmp/ccEMHPHa.o: In function `main':
main.cpp:(.text+0x46): undefined reference to `cv::imread(cv::String const&, int)'
/tmp/ccEMHPHa.o: In function `cv::String::String(char const*)':
main.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x58): undefined reference to `cv::String::allocate(unsigned long)'
/tmp/ccEMHPHa.o: In function `cv::String::~String()':
main.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallocate()'
/tmp/ccEMHPHa.o: In function `cv::Mat::~Mat()':
main.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to `cv::fastFree(void*)'
/tmp/ccEMHPHa.o: In function `cv::Mat::release()':
main.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x4b): undefined reference to `cv::Mat::deallocate()'
collect2: error: …
Run Code Online (Sandbox Code Playgroud)

c++ opencv

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

for循环结果0在C++中

我是C++的新手,两周前我开始用C++学习编码.为什么我的代码在构建和运行时总是给我结果0?请帮忙

# include <iostream>
# include <string>

using namespace std;

int main ()
{
    int input = 1;
    cout << "input your number : \n";
    cin >> input;

    int faktorial = 1;

    for(int i=1;i<=input;i++)
    {
        faktorial = faktorial * i;
    }
    cout << "factorial value from number  " << input << " is " << faktorial << endl;
}
Run Code Online (Sandbox Code Playgroud)

c++ loops for-loop

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

为什么添加函数参数会导致SLOT()无法识别?

我有一个课程如下:

handler.h中:

#ifndef HANDLER_H
#define HANDLER_H

#include <QObject>

class handler : public QObject
{
    Q_OBJECT

public:
    explicit handler(QObject *parent = nullptr);
    ~handler();

public slots:
    void returnHandler(int input);
};

#endif // HANDLER_H
Run Code Online (Sandbox Code Playgroud)

handler.cpp:

#include "handler.h"
#include "otherclass.h"

handler::handler(QObject *parent) : QObject(parent)
{

}

handler::~handler()
{

}

void handler::returnHandler(int input)
{
    otherclass *otherclassPointer = otherclass::getInstance();
    otherclassPointer->returnFunction(input);
}
Run Code Online (Sandbox Code Playgroud)

如图所示,这是一个非常简单的类,旨在接收输入并将输入传递给外部类('otherclass')中的函数.在我的主应用程序('main.cpp')中,我创建了一个QThread,并returnHandlerQThread启动时调用插槽,如下所示:

main.cpp中:

QThread* newThread = new QThread();
handler* handlerPointer = new handler();
handlerPointer->moveToThread(newThread);
connect(newThread, SIGNAL(started()), handlerPointer, SLOT(returnHandler(someInput)));
newThread->start();
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是: …

c++ qt signals-slots

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

C++ constexpr重载具有相同名称的构造函数

我确实理解constexpr在运行时可以计算的表达式上使用它时的用法.

我想为复数创建一个constexpr.x = 5_i应该创建一个复杂数量的我自己创建的复杂类,并且我需要一个constantexpr constructor.

class Complex {
private:
    double real_;
    double imag_;

public:
    ...
    Complex(double real, double imaginary);

    constexpr Complex(double real, double imaginary):
        real_(real),imag_(imaginary) {};

//Nonmember function
constexpr Complex operator""_i(long double arg);
Run Code Online (Sandbox Code Playgroud)

Complex(double real, double imaginary);稍后在.cpp文件中定义的.
当我尝试编译它时,我收到以下错误:

‘constexpr Complex::Complex(double, double)’ cannot be overloaded with  
‘Complex::Complex(double, double)’
Run Code Online (Sandbox Code Playgroud)

如果我只定义constexpr函数我的结论是我不能Complex::Complex(double, double)在运行时使用.

为什么我不能定义两个不同的功能?这在C++中是不允许的?编译器能否看到两个函数之间的区别?还有其他办法吗?

c++ overloading class constexpr

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

list :: sort更改值c ++

我在c ++中编写了一个不起作用的简单算法.我发现当我从我自己制作的自定义类中排序指向对象的列表时,它们会发生变化.或者更确切地说,列表更改为该类中的奇怪随机对象.

// ConsoleApplication45.cpp : Defines the entry point for the console application.
//


#include "stdafx.h"
#include <iostream>  
#include <map>
#include <list>
#include <math.h> 
using namespace std;  

class LocationNode;
class NodeMap;


class LocationNode
{
private:
    char name;
    int xLocation;
    int yLocation;
    //map<LocationNode*,int> neighbors;
    LocationNode* neighbors[100];
    int neighborWeight[100];
    int neighborCount; 
    LocationNode *previous;
    int score;

    int CalcDistance(LocationNode &dest)
    {
        return (int)sqrt(pow(dest.xLocation-xLocation,2) + pow(dest.yLocation-yLocation,2));
    }

public:
    int finalScore;

    bool operator<(LocationNode const& rhs) const
    {
       // Use whatever makes sense for your application.
       return …
Run Code Online (Sandbox Code Playgroud)

c++ sorting visual-studio

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