Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

this version is a developed one for my last question. The errors in the last article are already solved. But a new small bug occurs.
Recently problem: class can only be declared and used within an event.

Code follwing:

CamShow.cpp:
#include "stdafx.h"
#include <DShow.h>
#include <Windows.h>
#include <comdef.h>
#include "CamShow.h"

CamShow::CamShow()
        {		
			pGraphBuilder				= NULL; 
			pCGB					= NULL;
			pCGB2					= NULL;
			......
	}
	CamShow::~CamShow() {}
	
        HRESULT CamShow::InitComLib()
	{
		HRESULT hr = NULL;
		hr = CoInitialize(NULL);
		return hr;
	}
		
	HRESULT CamShow::CaptureVideo(int NO)
	{
		HRESULT hr;
		IBaseFilter *pSrcFilter=NULL;
		......
				}
        //other functions
        //.
        //.
        //.

CamShow.h:
#pragma once //with or without no difference
#include <DShow.h>
#include <Windows.h>
#include <comdef.h>

#define WM_GRAPHNOTIFY  WM_APP+1
enum VIDEOSTATUS {Stopped, Paused, Running, Init};

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

	public:	
			HRESULT CamShow::InitComLib();
			HRESULT CamShow::CaptureVideo(int);
			......
                        void CamShow::CoUnini();

      	public:
			IGraphBuilder			*pGraphBuilder;
			ICaptureGraphBuilder		*pCGB;
			ICaptureGraphBuilder2		*pCGB2;
			IVideoWindow			*pVW;
			IMediaControl				*pMC;
			IMediaEventEx				*pME;
                        ......
				
        };
Form1.h:
#pragma once
#include "Form2.h"
......
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                Form^ frm = gcnew Form2();
                frm->Show();
             }
......


Form2.h:
#pragma once
#include "CamShow.h"

namespace LOSCH {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
        
        //CamShow test;   
// when i declare a class test, the system error says:
// error LNK2005: "class WebcamShow::CamShow test" (?test@@3VCamShow@WebcamShow@@A) is already in Form2.obj defined.
	
	/// <summary>
	/// Zusammenfassung für Form2
	/// </summary>
	public ref class Form2 : public System::Windows::Forms::Form
	{
		private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
				CamShow test;
// This works well and I can get the video to be captured on form2
// But the problem is, the test class is only within this section useful,
// e.g. when I want to use another button2_click to stop the preview, I have to make the declaration test again and all initializations and querys etc. there again. At last with pMC->Stop(); ... Which is obviously not good. 
				button1->Enabled=false; 

					hr = test.InitComLib();

					hr = test.GetInterfaces();
                                        .......

}
Posted

Try:
C#
public ref class Form2 : public System::Windows::Forms::Form
{
    // make test available to all following methods
    CamShow test;

    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
 
Share this answer
 
Comments
christmars 5-Oct-12 9:54am    
I've tried that, but it still makes error.
Richard MacCutchan 5-Oct-12 12:47pm    
And?
christmars 6-Oct-12 4:38am    
And the same error...
Richard MacCutchan 6-Oct-12 5:15am    
You have a global definition somewhere in your Form2.
christmars 6-Oct-12 5:37am    
I've taken the Form2.h in Form2.cpp away. And the global definition of the class works. Do you think this is right in win form app?
You have multiply declaration of the variable with same name in the same scope.
Try to use different name of the variable if you need it in scope there you try to declare it.

Like

C++
WebcamShow::CamShow Form2Test;


And use it in you methods in Form2. But better to declare variables in a class not globally.

Regards,
Maxim.
 
Share this answer
 
Comments
christmars 8-Oct-12 1:53am    
Hi Maxim! Firstly sorry for my late reply and thank you so much for your answer. But there's nothing to do with a "different name". That's not the point, I have to say. The problem is that I want to declare and make use of a "global" class and it shows error that the declaration is duplacated.
Form1.cpp/Form1.h includes Form2.h and creates a global CamShow object. Form2.cpp includes also Form2.h and creates another CamShow object with the same name in the same namespace. So I took the Form2.h in Form2.cpp away and it works well.
But actrually I am a Win Form APP beginner. So I'm not sure if this is a right way for stable programms in this windows forms app.
Maxim Kartavenkov 8-Oct-12 2:03am    
then you should declare you object in Form1.h as extern: "extern WebcamShow::CamShow test;" - that means object declaration located somethere but not here. In Form1.cpp declare that object: "WebcamShow::CamShow test;" - same line without "extern" and then include your Form1.h there you need your class. - follow?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900