包含的头文件未被看到
问题描述:
编辑:新文件。我遇到了在Form1类中访问公共函数的麻烦。当我尝试使用它时,我找不到标识符。 Form1中:包含的头文件未被看到
#pragma once
#include "OpenGL.h"
#include "serialcom.h"
#include "calculations.h"
namespace GUI_1 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace OpenGLForm;
/// <summary>
/// Summary for Form1
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
OpenGL = gcnew COpenGL(this->panel4, this->label16, 785, 530);
}
void changelabel2(float num)
{
label2 -> Text = " " + num;
}
protected: ...
OpenGL.h:
#include "stdafx.h"
#ifndef opengl
#define opengl
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <math.h>
// Declare globals
...
using namespace System::Windows::Forms;
namespace OpenGLForm
{
public ref class COpenGL: public System::Windows::Forms::NativeWindow
{
public:
COpenGL(System::Windows::Forms::Panel^parentForm, System::Windows::Forms::Label^lbl, GLsizei iWidth, GLsizei iHeight)
{
CreateParams^ cp = gcnew CreateParams;
c_p_v v1, v2;
changelabel2(189);
...
所以不工作(以上,在 “changelabel2”)。也许是因为我没有使用类名?
这是我的主:
#include "stdafx.h"
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <vcclr.h>
#include <stdio.h>
#include <stdlib.h>
#include <vcclr.h>
#include "Form1.h"
#include "calculations.h"
#include "serialcom.h"
using namespace GUI_1;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
Form1^ form = gcnew Form1();
Application::Run(form);
return 0;
}
调用form.changelabel2也不起作用。
答
看起来你有 OpenGL.h和Form1.h
之间的循环依赖尝试取出#include "Form1.h"
如果可以的话,或将其转换成一个向前声明一样class Form1;
同时,应注意当在标题中使用using namespace
,因为它会污染随后包含的任何文件的名称空间。
谢谢凯尔。我现在有另一个问题。我想我会先在这里尝试。 我在我的Form1类中有一个公共职能 - 其目的是通过被外部调用来更新标签。但是,我的OpenGL类似乎无法访问它。我在下面发布了更多代码,因为我觉得这个小窗口很难使用。 – Mewa 2012-07-17 01:47:40
它看起来像OpenGL类可能无法访问Form1类,因为没有'#include“Form1.h”'这是您将声明与实现分开的原因之一(典型情况下,.h和.cpp/.cc) 。如果您将实现保留在头文件之外,那么编写无循环依赖的工作代码要容易得多,因为头文件不需要'#inc',并且(通常)不会将它放在源文件中。 – Kyle 2012-07-18 18:56:13