标签: visual-c++

从路径中获取文件名

从路径获取文件名的最简单方法是什么?

string filename = "C:\\MyDirectory\\MyFile.bat"
Run Code Online (Sandbox Code Playgroud)

在这个例子中,我应该得到"MyFile".没有延期.

c++ visual-c++

72
推荐指数
11
解决办法
17万
查看次数

是否正确使用#pragma warning push/pop来暂时改变警告级别?

偶尔编写一下根本不会发出警告的C++代码是很困难的.然而,启用警告是一个好主意.因此,通常需要禁用某些特定构造周围的警告,并在所有其他代码段中启用它们.

到目前为止,我已经看到了两种方法.

第一个是使用#pragma warning( push )#pragma warning( pop ):

 #pragma warning( push )
 #pragma warning( disable: ThatWarning )
 //code with ThatWarning here
 #pragma warning( pop )
Run Code Online (Sandbox Code Playgroud)

第二是使用#pragma warning( default ):

 #pragma warning( disable: ThatWarning )
 //code with ThatWarning here
 #pragma warning( default: ThatWarning )
Run Code Online (Sandbox Code Playgroud)

我在第二个变体中看到的问题是它丢弃了原始警告级别 - 警告可能在此之前已关闭或警告级别可能已被更改.使用default会丢弃这些改动.

第一种方法看起来很干净.它有什么问题吗?有没有更好的方法来实现同样的目标?

c++ warnings pragma compiler-warnings visual-c++

70
推荐指数
4
解决办法
5万
查看次数

PCH警告:标头停止不能在宏或#if块中 - Visual C++ 2010 Express SP1

这是从一个可能正在运作的网站上粘贴的.我做了一些谷歌搜索,发现我现在的问题是我今天下载的Visual C++ 2010 SP1的结果,现在给我这个错误:

PCH Warning: header stop cannot be in a macro or #if block.

希望有人能够帮助我!

#ifndef APP_STATE_H
#define APP_STATE_H

#include "Framework.h"

class AppState; //this line is giving me the error

//define two classes

#endif
Run Code Online (Sandbox Code Playgroud)

Framework.h:

#ifndef OGRE_FRAMEWORK_H
#define OGRE_FRAMEWORK_H

#include <OgreCamera.h>
#include <OgreEntity.h>
#include <OgreLogManager.h>
#include <OgreOverlay.h>
#include <OgreOverlayElement.h>
#include <OgreOverlayManager.h>
#include <OgreRoot.h>
#include <OgreViewport.h>
#include <OgreSceneManager.h>
#include <OgreRenderWindow.h>
#include <OgreConfigFile.h>

#include <OISEvents.h>
#include <OISInputManager.h>
#include <OISKeyboard.h>
#include <OISMouse.h>

class OgreFramework : public Ogre::Singleton<OgreFramework>,OIS::KeyListener,OIS::MouseListener{
public:
    OgreFramework();
    ~OgreFramework();

    bool …
Run Code Online (Sandbox Code Playgroud)

c++ visual-c++

68
推荐指数
3
解决办法
6万
查看次数

Visual Studio:我可以复制项目的属性以在另一个项目中使用吗?

我添加了几个目录,如matlab,opencv等库来编译我在Visual Studio项目中的当前C文件.

我即将推出的所有项目都需要相同的设置/属性.我是否需要单独设置每个项目的属性,或者我可以做些什么来将它应用于所有项目?

visual-studio-2010 visual-c++

67
推荐指数
1
解决办法
6万
查看次数

fopen弃用警告

Visual Studio 2005 C++编译器上,当我的代码使用fopen和此类调用时,我收到以下警告.

1>foo.cpp(5) : warning C4996: 'fopen' was declared deprecated
1>        c:\program files\microsoft visual studio 8\vc\include\stdio.h(234) : see declaration of 'fopen'
1>        Message: 'This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'
Run Code Online (Sandbox Code Playgroud)

我该如何防止这种情况?

fopen deprecated visual-c++

66
推荐指数
5
解决办法
16万
查看次数

错误LNK2038:检测到'_MSC_VER'不匹配:值'1600'与CppFile1.obj中的值'1700'不匹配

我正在将我的项目从VS2010转换为VS2012.但是我在某些项目中遇到了_MSC_VER链接器错误.经过长时间浏览谷歌后,我发现问题是由于VS2010中创建的库与VS2012的链接.

如何找出导致错误的项目?我在这里引用错误:

Error   6   error LNK2038: mismatch detected for '_MSC_VER': value '1600' doesn't match value '1700' in CppFile1.obj      D:\ProjectLocation\Projectname1.lib(CppFile2.obj) Projectname2
Error   15  error LNK2001: unresolved external symbol "private: static void __cdecl std::locale::facet::_Facet_Register(class std::locale::facet *)" (?_Facet_Register@facet@locale@std@@CAXPAV123@@Z)  D:\ProjectLocation\Projectname1.lib(CppFile3.obj)   Projectname2
Error   13  error LNK2038: mismatch detected for '_MSC_VER': value '1600' doesn't match value '1700' in CppFile1.obj    D:\ProjectLocation\Projectname1.lib(CppFile4.obj)   Projectname2
Error   12  error LNK2038: mismatch detected for '_MSC_VER': value '1600' doesn't match value '1700' in CppFile1.obj    D:\ProjectLocation\Projectname1.lib(CppFile5.obj)   Projectname2
Error   10  error LNK2038: mismatch detected for …
Run Code Online (Sandbox Code Playgroud)

c++ msbuild visual-studio-2010 visual-c++

65
推荐指数
2
解决办法
13万
查看次数

为什么不允许使用静态const浮点数?

我有一个类,它基本上只包含我的应用程序使用的一堆常量定义.但出于某种原因,longs编译但float不是:

class MY_CONSTS
{
public :
    static const long   LONG_CONST = 1;      // Compiles 
    static const float FLOAT_CONST = 0.001f; // C2864
};
Run Code Online (Sandbox Code Playgroud)

给出以下错误:

1>c:\projects\myproject\Constant_definitions.h(71) : error C2864: 'MY_CONSTS::FLOAT_CONST' : only static const integral data members can be initialized within a class
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?

c++ visual-c++

64
推荐指数
5
解决办法
4万
查看次数

/ Ox和/ O2编译器选项有什么区别?

Microsoft的C++编译器(cl.exe包含在Visual Studio中)提供了几个优化开关.大多数它们之间的差异似乎是不言自明的,但我不清楚它之间的区别/O2(它优化代码以获得最大速度)和/Ox(选择"完全优化").

我试着阅读文档/Ox选项,它似乎证实,该交换机还支持优化的最高速度,而不是大小:

所述/Ox编译器选项产生在较小尺寸有利于代码的执行速度.

但特别是,"备注"部分下面的声明引起了我的注意:

通常,指定/O2(最大化速度)而不是/Ox.

所以我的问题是,为什么要一个普遍青睐/O2/Ox后一个选项是否允许已知的特定优化导致无法预料的错误或其他意外行为?是否只是获得的优化量不值得额外的编译时间?或者这只是一个完全没有意义的"推荐",因为它/O2是VS中的默认选项?

c++ compiler-optimization compiler-options visual-studio visual-c++

64
推荐指数
2
解决办法
2万
查看次数

如何将迭代器增加2?

谁能告诉我如何将迭代器增加2?

iter++可用 - 我必须这样做iter+2吗?我怎样才能做到这一点?

c++ iterator stl visual-c++

63
推荐指数
6
解决办法
8万
查看次数

memset()或值初始化将结构清零?

在Win32 API编程中,通常使用struct具有多个字段的C s.通常只有其中几个具有有意义的值,而其他所有值都必须归零.这可以通过以下两种方式之一实现:

STRUCT theStruct;
memset( &theStruct, 0, sizeof( STRUCT ) );
Run Code Online (Sandbox Code Playgroud)

要么

STRUCT theStruct = {};
Run Code Online (Sandbox Code Playgroud)

第二个变体看起来更干净 - 它是一个单行,它没有任何可能输错的参数并导致错误被种植.

与第一个变体相比,它有任何缺点吗?使用哪种变体?为什么?

c c++ struct initialization visual-c++

63
推荐指数
6
解决办法
7万
查看次数