error LNK2001: unresolved external symbol public: static class [duplicate]

别说谁变了你拦得住时间么 提交于 2019-12-24 07:29:04

问题


I cannot figure out why i am recieving this error. Can anyone lend a hand. I need to declare VideoCapture capture in the header file and call it in Video.cpp

Video.h

class Video
{
    public:

    static VideoCapture capture;

    //Default constructor
    Video();

    //Declare a virtual destructor:
    virtual ~Video();

    //Method
    void Start();   

    private:
};

Video.cpp

#include "StdAfx.h"
#include "Video.h"
#include "UserInfo.h"
#include "Common.h"

void Video::Start()
{
  while(1)
  {
    Mat img;

    bool bSuccess = capture.read(img); // read a new frame from video

     if (!bSuccess) //if not success, break loop
    {
                    cout << "End of video" << endl;
                   break;
    }

    imshow("original video", img); //show the frame in "Original Video" window

    if(waitKey(30) == 27) //wait for 'esc' key press for 30 ms. If 'esc' key is pressed, break loop
   {
            cout << "esc key is pressed by user" << endl; 
            break; 
   }
  }
}

Any help would be greatly appreciated


回答1:


In a class definition, static variables are a merely a declaration. You have only declared that capture will exist somewhere.

You need to add the definition. Make the variable exist.

In any version of C++

You can separately define the variable in your cpp file.

const VideoCapture Video::capture;

In C++ 17 or later

You can declare the variable inline in your header to make it a definition.

static inline VideoCapture capture;


来源:https://stackoverflow.com/questions/24622441/error-lnk2001-unresolved-external-symbol-public-static-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!