gpt4 book ai didi

c++ - 如何在 Visual C++ Windows 窗体应用程序中包含文件

转载 作者:行者123 更新时间:2023-11-28 06:10:16 24 4
gpt4 key购买 nike

我创建了一个新的 Windows 窗体应用程序。

之后,我添加了一个新表单。在我的表单中,我添加了 Form_Load 事件(通过使用表单编辑器)。

据我所知 - Visual Studio 生成了 MainForm.c 和 MainForm.h 文件。我必须编辑 MainForm.c,向其中添加主要功能等。

我还创建了一个名为 CVEngine 的类。

我想做什么

我想在我的 MainForm 中创建 CVEngine 类的实例(请参阅我的 MainForm.h 文件,文件末尾的 MainForm_Load 函数)。

我的问题是什么?

我想我的包含有问题。我不太了解 C++,这是我的第一个 Windows 窗体 C++ 应用程序。我有一点 C#、PHP 和一点 Java 的背景。我对C++和头文件有基本的了解,但是Visual Studio生成的这个MainForm.h很奇怪(.h文件中有代码),我有点迷茫。

1>------ Build started: Project: Sensor Cam, Configuration: Debug Win32 ------
1> CVEngine.cpp
1> MainForm.cpp
1> Generating Code...
1>CVEngine.obj : error LNK2005: "public: __thiscall CVEngine::CVEngine(void)" (??0CVEngine@@QAE@XZ) already defined in MainForm.obj
1>CVEngine.obj : error LNK2005: "public: __thiscall CVEngine::~CVEngine(void)" (??1CVEngine@@QAE@XZ) already defined in MainForm.obj
1>CVEngine.obj : error LNK2005: "public: __thiscall CVEngine::CVEngine(void)" (??0CVEngine@@$$FQAE@XZ) already defined in MainForm.obj
1>CVEngine.obj : error LNK2005: "public: __thiscall CVEngine::~CVEngine(void)" (??1CVEngine@@$$FQAE@XZ) already defined in MainForm.obj
1>CVEngine.obj : warning LNK4248: unresolved typeref token (0100001A) for 'CvCapture'; image may not run
1>MainForm.obj : warning LNK4248: unresolved typeref token (01000021) for 'CvCapture'; image may not run
1>CVEngine.obj : error LNK2028: unresolved token (0A00052F) "extern "C" struct CvCapture * __cdecl cvCreateCameraCapture(int)" (?cvCreateCameraCapture@@$$J0YAPAUCvCapture@@H@Z) referenced in function "public: __thiscall CVEngine::CVEngine(void)" (??0CVEngine@@$$FQAE@XZ)
1>MainForm.obj : error LNK2028: unresolved token (0A000532) "extern "C" struct CvCapture * __cdecl cvCreateCameraCapture(int)" (?cvCreateCameraCapture@@$$J0YAPAUCvCapture@@H@Z) referenced in function "public: __thiscall CVEngine::CVEngine(void)" (??0CVEngine@@$$FQAE@XZ)
1>CVEngine.obj : error LNK2028: unresolved token (0A00065F) "int __cdecl cv::_interlockedExchangeAdd(int *,int)" (?_interlockedExchangeAdd@cv@@$$FYAHPAHH@Z) referenced in function "public: void __thiscall cv::SparseMat::addref(void)" (?addref@SparseMat@cv@@$$FQAEXXZ)
1>MainForm.obj : error LNK2028: unresolved token (0A000672) "int __cdecl cv::_interlockedExchangeAdd(int *,int)" (?_interlockedExchangeAdd@cv@@$$FYAHPAHH@Z) referenced in function "public: void __thiscall cv::SparseMat::addref(void)" (?addref@SparseMat@cv@@$$FQAEXXZ)
1>CVEngine.obj : error LNK2020: unresolved token (0A00066D) "public: void __thiscall cv::Mat::copySize(class cv::Mat const &)" (?copySize@Mat@cv@@$$FQAEXABV12@@Z)
1>MainForm.obj : error LNK2028: unresolved token (0A000680) "public: void __thiscall cv::Mat::copySize(class cv::Mat const &)" (?copySize@Mat@cv@@$$FQAEXABV12@@Z) referenced in function "public: __thiscall cv::Mat::Mat(class cv::Mat const &)" (??0Mat@cv@@$$FQAE@ABV01@@Z)
1>MainForm.obj : error LNK2019: unresolved external symbol "int __cdecl cv::_interlockedExchangeAdd(int *,int)" (?_interlockedExchangeAdd@cv@@$$FYAHPAHH@Z) referenced in function "public: void __thiscall cv::SparseMat::addref(void)" (?addref@SparseMat@cv@@$$FQAEXXZ)
1>CVEngine.obj : error LNK2001: unresolved external symbol "int __cdecl cv::_interlockedExchangeAdd(int *,int)" (?_interlockedExchangeAdd@cv@@$$FYAHPAHH@Z)
1>MainForm.obj : error LNK2019: unresolved external symbol "extern "C" struct CvCapture * __cdecl cvCreateCameraCapture(int)" (?cvCreateCameraCapture@@$$J0YAPAUCvCapture@@H@Z) referenced in function "public: __thiscall CVEngine::CVEngine(void)" (??0CVEngine@@$$FQAE@XZ)
1>CVEngine.obj : error LNK2001: unresolved external symbol "extern "C" struct CvCapture * __cdecl cvCreateCameraCapture(int)" (?cvCreateCameraCapture@@$$J0YAPAUCvCapture@@H@Z)
1>MainForm.obj : error LNK2019: unresolved external symbol "public: void __thiscall cv::Mat::copySize(class cv::Mat const &)" (?copySize@Mat@cv@@$$FQAEXABV12@@Z) referenced in function "public: __thiscall cv::Mat::Mat(class cv::Mat const &)" (??0Mat@cv@@$$FQAE@ABV01@@Z)
1>CVEngine.obj : error LNK2001: unresolved external symbol "public: void __thiscall cv::Mat::copySize(class cv::Mat const &)" (?copySize@Mat@cv@@$$FQAEXABV12@@Z)
1>e:\documents\visual studio 2013\Projects\Sensor Cam\Debug\Sensor Cam.exe : fatal error LNK1120: 9 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

MainForm.h 文件:

#pragma once

#include "CVEngine.cpp"

namespace SensorCam {

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
// using namespace System::Data;
using namespace System::Drawing;

/// <summary>
/// Summary for MainForm
/// </summary>
public ref class MainForm : public System::Windows::Forms::Form
{
public:
MainForm(void)
{
InitializeComponent();
//
// TODO: Add the constructor code here
//
}

protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~MainForm()
{
if (components)
{
delete components;
}
}

private:
System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->SuspendLayout();
//
// MainForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(633, 418);
this->Name = L"MainForm";
this->Text = L"Sensor Cam";
this->Load += gcnew System::EventHandler(this, &MainForm::MainForm_Load);
this->ResumeLayout(false);
}
#pragma endregion

private: System::Void MainForm_Load(System::Object^ sender, System::EventArgs^ e)
{
CVEngine cve = CVEngine::CVEngine();
}

};
}

MainForm.cpp 文件:

#include "MainForm.h"

using namespace System;
using namespace System::Windows::Forms;

[STAThread]
void main(int argc, char *argv[])
{
Application::EnableVisualStyles();
// Application::SetCompatibleTextRenderingDefault(false);

SensorCam::MainForm form;
Application::Run(%form);
}

CvEngine.cpp 文件:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;

class CVEngine
{
public:
CVEngine();
~CVEngine();

private:
CvCapture* capture;
};

CVEngine::CVEngine()
{
capture = cvCaptureFromCAM(-1);
if (!capture)
{
// Todo: error handling
}
}

CVEngine::~CVEngine()
{

}

最佳答案

要让它编译,创建一个cv_engine.h文件

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;

class CVEngine
{
public:
CVEngine();
~CVEngine();

private:
CvCapture* capture;

};

cv_engine.cpp

#include "cv_engine.h"

CVEngine::CVEngine()
{
capture = cvCaptureFromCAM(-1);
if (!capture)
{
// todo: error handling
}
}

CVEngine::~CVEngine()
{

}

然后在Mainform.h中#include "cv_engine.h"

另外,你真的不应该在头文件中做这样的事情......

使用命名空间 cv;

什么时候可以只使用作用域...

cv::CvCapture* 捕获;

关于c++ - 如何在 Visual C++ Windows 窗体应用程序中包含文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31401858/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com