Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ .NET WinForm
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 5-Oct-12 1:22am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try:
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) {
  Permalink  
Comments
christmars at 5-Oct-12 9:54am
   
I've tried that, but it still makes error.
Richard MacCutchan at 5-Oct-12 12:47pm
   
And?
christmars at 6-Oct-12 4:38am
   
And the same error...
Richard MacCutchan at 6-Oct-12 5:15am
   
You have a global definition somewhere in your Form2.
christmars at 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?
Richard MacCutchan at 6-Oct-12 11:58am
   
The issue is that you should have the definition in one place only, so I would say that you are correct.
christmars at 8-Oct-12 1:56am
   
Sorry for my late rely! Thanks!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
 
WebcamShow::CamShow Form2Test;
 
And use it in you methods in Form2. But better to declare variables in a class not globally.
 
Regards,
Maxim.
  Permalink  
Comments
christmars at 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 at 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)

  Print Answers RSS
0 OriginalGriff 259
1 Sergey Alexandrovich Kryukov 182
2 Hard_Rockz 153
3 Richard MacCutchan 125
4 Maciej Los 104
0 OriginalGriff 5,374
1 Sergey Alexandrovich Kryukov 4,713
2 Peter Leow 2,944
3 DamithSL 2,465
4 Maciej Los 2,270


Advertise | Privacy | Mobile
Web04 | 2.8.140718.1 | Last Updated 6 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid