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

I want to realize this thing:
Form1(Button_Click)-> Form2(Butto_Click)-> Video preview from webcam.

what I already can:
Form1(Button_Click)-> Video Preview.

I easily make form2.h under form1 and CamShow.h under form2.
But i got this error which says:
error LNK2005: "public: __thiscall WebcamShow::CamShow::CamShow(void)" (??0CamShow@WebcamShow@@QAE@XZ) is already in Form2.obj defined.
(and all the methods i declared in CamShow are mentioned just the same)

Codes for CamShow.h:
C++
#include <DShow.h>
#include <Windows.h>
#include <comdef.h>

#define WM_GRAPHNOTIFY  WM_APP+1
enum VIDEOSTATUS {Stopped, Paused, Running, Init};
namespace WebcamShow
{
	public class CamShow
	{
	public: CamShow(); 
		~CamShow();

	public:		HRESULT CamShow::InitComLib();
			HRESULT CamShow::CaptureVideo(int);
			......
	public: 
			IGraphBuilder			*pGraphBuilder;
			ICaptureGraphBuilder		*pCGB;
			ICaptureGraphBuilder2		*pCGB2;
			IVideoWindow			*pVW;

			IMediaControl			*pMC;
			IMediaEventEx			*pME;
                        ......
			
	};

	CamShow::CamShow()
	{		
			pGraphBuilder				= NULL; 
			pCGB					= NULL;
			pCGB2					= NULL;
			pVW					= NULL;

			pMC					= NULL;
			pME					= NULL;

			......
	}
	CamShow::~CamShow() {}

	HRESULT CamShow::InitComLib()
	{
		HRESULT hr = NULL;
		hr = CoInitialize(NULL);
		return hr;
	}
        ......	 


Form1:
C++
#pragma once
#include "Form2.h"
......
And Form2:
C++
#pragma once
#include "CamShow.h"
......


i think the problem is about duplicated declarations. And i've read the msdn explaination for this error, which i don't quite understand.
How can I solve this?

Thanks!!!
Posted
Updated 3-Oct-12 23:16pm
v2

1 solution

Your your first code block CamShow.h is a header file containing not only declarations but also code that will be compiled each time the file is included. So you will get the same code compiled multiple times and stored in different object files. The linker then generates the error.

You should move the code sections (constructor, destructor, functions) into an own source file CamShow.cpp. Optionally you may declare short functions as inline.
 
Share this answer
 
Comments
christmars 4-Oct-12 8:35am    
Hi Jochen,

i used inline but another problem happens: i can't create a class. It tells me:
error LNK2005: "class WebcamShow::CamShow test" (?test@@3VCamShow@WebcamShow@@A) is already in Form2.obj defined.
And when i do the .cpp, there is the same problem!
Jochen Arndt 4-Oct-12 9:00am    
Did you have a global CamShow object in some header file (a line containing 'CamShow test;')? Then this would result in this error. Depending on your project the object should be not global but part of another class (Form1 and/or Form2 or the app/mainframe/document class).

It seems that you have mixed up definitions (code implementation) and declarations.

But it is hard to help without knowing more details. Using the provided details, I suggest to add the CamShow object as a member to your app, main frame, or document class and provide functions to access the member from your forms.

christmars 4-Oct-12 9:30am    
No, i didn't.
But it works when I just put the class definition within the button_click event.
I definitated it after "using namespace WebcamShow;" earlier and it had error.
Jochen Arndt 4-Oct-12 12:22pm    
I still can not imagine how you use the class. But I just recognized that you used the '#pragma once' directives followed by include statements. Are these pragmas in the .cpp source files? They are for usage with header files and you should add one to your CamShow.h file.
christmars 4-Oct-12 15:25pm    
I made the declaration of a class(CamShow test) within button_click event. It seems like that the declaration outside or in public sections caused error. But it's not good when the defination of the class is within the event block. Because i can't access it all over in the form2.

To your question: no, my .cpp doesn't have pragma once in it. So does CamShow.h. I'll try to add this in .h to see, if this matters.

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