如何防止包含两次头文件?问题是我包括了在MyClass.h中然后我将MyClass.h包含在许多文件中,因此它包含多次并发生重定义错误.怎么预防?
我正在使用#pragma而不是包含警卫,我想这很好.
MyClass.h:
// MyClass.h
#pragma once
#include <winsock2.h>
class MyClass
{
// methods
public:
MyClass(unsigned short port);
virtual ~MyClass(void);
};
Run Code Online (Sandbox Code Playgroud)
编辑:我得到的错误很少
c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(91) : warning C4005: 'AF_IPX' : macro redefinition
c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(460) : see previous definition of 'AF_IPX'
c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(124) : warning C4005: 'AF_MAX' : macro redefinition
c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(479) : see previous definition of 'AF_MAX'
c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(163) : warning C4005: 'SO_DONTLINGER' : macro redefinition
c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(402) : …Run Code Online (Sandbox Code Playgroud) 我正在运行一些Ruby代码,每次日期更改时都会破坏Ruby文件.在文件中,我有不断的定义,比如
Tau = 2 * Pi
Run Code Online (Sandbox Code Playgroud)
当然,它们使解释器每次都显示不需要的"已初始化的常量"警告,所以,我想要具备以下功能:
def_if_not_defined(:Tau, 2 * Pi)
redef_without_warning(:Tau, 2 * Pi)
Run Code Online (Sandbox Code Playgroud)
通过编写我的所有常量定义,我可以避免警告:
Tau = 2 * Pi unless defined?(Tau)
Run Code Online (Sandbox Code Playgroud)
但它不够优雅,有点潮湿(不是干).
有更好的方法def_if_not_defined吗?怎么样redef_without_warning?
-
谢谢Steve的解决方案:
class Object
def def_if_not_defined(const, value)
mod = self.is_a?(Module) ? self : self.class
mod.const_set(const, value) unless mod.const_defined?(const)
end
def redef_without_warning(const, value)
mod = self.is_a?(Module) ? self : self.class
mod.send(:remove_const, const) if mod.const_defined?(const)
mod.const_set(const, value)
end
end
A = 1
redef_without_warning :A, 2
fail 'unit test' unless A == …Run Code Online (Sandbox Code Playgroud) 考虑以下示例:
#include <iostream>
using namespace std;
class base
{
public:
virtual int func()
{
cout << "vfunc in base class\n";
return 0;
}
};
class derived: public base
{
public:
double func()
{
cout << "vfunc in derived class\n";
return 0;
}
};
int main()
{
base *bptr = new derived;
bptr->func();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译器为上面的代码提供了一个错误,即覆盖函数存在冲突类型.为什么不能使用不同的返回类型覆盖派生类中的函数?
我相信,为了覆盖一个函数,需要在派生类中重新定义基类虚方法.要重新定义方法,方法的签名必须相同.由于返回类型不是签名的一部分,我相信即使返回类型有差异,该方法仍将被重新定义?在上述代码的情况下,func在派生类中使用不同的返回类型重新定义虚函数.但是编译器会抛出错误.我的理解是否正确?
代码转储道歉:
gameObject.cpp:
#include "gameObject.h"
class gameObject
{
private:
int x;
int y;
public:
gameObject()
{
x = 0;
y = 0;
}
gameObject(int inx, int iny)
{
x = inx;
y = iny;
}
~gameObject()
{
//
}
int add()
{
return x+y;
}
};
Run Code Online (Sandbox Code Playgroud)
gameObject.h:
class gameObject
{
private:
int x;
int y;
public:
gameObject();
gameObject(int inx, int iny);
~gameObject();
int add();
};
Run Code Online (Sandbox Code Playgroud)
错误:
||=== terrac, Debug ===|
C:\terrac\gameObject.cpp|4|error: redefinition of `class gameObject'|
C:\terrac\gameObject.h|3|error: previous definition of `class gameObject'| …Run Code Online (Sandbox Code Playgroud) 我正在使用Visual Studio 2010.我正在尝试在OpenGL中编写简单的Camera类.我需要在Camera.h中包含gl/gl.h
gl/gl.h已经包含在main.cpp中了,Camera.h包含在main.cpp中当我放入
#include <gl/gl.h>
Run Code Online (Sandbox Code Playgroud)
在Camera.hi中有一堆像这样的错误:
Error 11 error C2086: 'int APIENTRY' : redefinition C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\gl\GL.h 1153 1 Gaz 3D
文件:
Camera.h
#include <math.h>
#include <gl/gl.h>
#ifndef _CAMERA_H
#define _CAMERA_H
class Camera
{
private:
Camera();
public:
static Camera& getCamera();
float x, y, z, rotv, roth;
void moveForward(float n);
void moveBackward(float n);
void moveLeft(float n);
void moveRight(float n);
void lookUp(float n);
void lookDown(float n);
void lookLeft(float n);
void lookRight(float n);
};
#endif
Run Code Online (Sandbox Code Playgroud)
main.cpp中:
#include <windows.h>
#include <gl\gl.h>
#include …Run Code Online (Sandbox Code Playgroud) 我目前正在研究一个用MFC C++编写的已经开发的项目,并且我正面临着一个已经存在的定义宏的问题:
#define HEIGHT_TESTS 13
Run Code Online (Sandbox Code Playgroud)
我试图从代码中更改值,但我认为,因为它是一个预处理的定义,我无法做到这一点.有没有办法解决这个问题,而不必整体改变原始宏(因为它可能会影响程序的原始功能).我只是打算在一个特定的条件下改变它,在其他地方休息它仍然是相同的.
只是为了让每个人都知道,我显然尝试使用不同的宏定义和我打算使用的值(17),但没有运气.
任何帮助将非常感激.
我是C++的新手.
任何人都可以告诉我c ++中方法重写和虚函数概念之间的区别.
虚函数的功能可以在其派生类中被覆盖.在派生类中重新定义函数称为函数重写.
为什么我们实际上有虚函数?
我正在编译Visual C++ 2010中的项目,但是我遇到了一些Winsock重定义的问题.
首先我得到:
syntax error : identifier 'SOCKADDR_STORAGE'
Run Code Online (Sandbox Code Playgroud)
但是,如果我包含winsock或winsock2或ws2tcpip,我会收到许多错误:
error C2011: 'sockaddr' : 'struct' type redefinition
error C2011: 'WSAData' : 'struct' type redefinition
error C2011: 'linger' : 'struct' type redefinition
Run Code Online (Sandbox Code Playgroud) 我有个问题.我使用Visual Studio 2013并获得以下错误:
Error C2371: 'getgrundflaeche' redefinition: different basic types.
Run Code Online (Sandbox Code Playgroud)
我不知道为什么我得到这个错误.当我尝试调用该函数时,我得到与VS12相同的错误getgrundflaeche().
这是代码:
#include <stdio.h>
#define PI 3.14159265359
int main(void){
double h = 0, d = 0, r = 0, G = 0, V = 0, M = 0, O = 0;
printf("Geometrie Zylinder:\nBitte geben sie den Durchmesser d ein (cm): ");
scanf_s("%lf", &d);
printf("Bitte geben sie die Höhe h ein (cm): ");
scanf_s("%lf", &h);
r = d / 2;
G = getgrundflaeche(r);
/*V = get_volumen(r, h);
M = …Run Code Online (Sandbox Code Playgroud) 当我编译下面的代码时
#include<stdio.h>
int main()
{
int a;
int a = 10;
printf("a is %d \n",a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
test3.c: In function ‘main’:
test3.c:6:5: error: redeclaration of ‘a’ with no linkage
test3.c:5:5: note: previous declaration of ‘a’ was here
Run Code Online (Sandbox Code Playgroud)
但是,如果我将变量设为全局,那么它可以正常工作.
#include<stdio.h>
int a;
int a = 10;
int main()
{
printf("a is %d \n",a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么两次声明相同的全局变量而不是错误,但对局部变量这样做是错误的?
redefinition ×10
c++ ×6
c ×2
overriding ×2
class ×1
constants ×1
declaration ×1
definition ×1
header ×1
include ×1
macros ×1
mfc ×1
opengl ×1
redefine ×1
ruby ×1
windows ×1
winsock ×1