小编Guy*_*ean的帖子

"<<"和">>"运算符如何进行I/O操作?

可能重复:
运算符重载

我正在期待已久的回归C++,并且有一些基本符号在其他语言中看起来并不那么突出.

如果你看一下这行代码

cout << "firstvalue is " << firstvalue << endl;
Run Code Online (Sandbox Code Playgroud)

我意识到这是做什么的.它将"firstvalue is x"写入控制台.x是firstvalue的值.但是,我对"<<"或">>"双角括号一无所知.我无法研究它们或它们做什么,因为我不知道它们的正式名称.

我的问题是,上述陈述中实际发生了什么(一步一步)?这些"<<"是什么?我想我明白cout是用于写入控制台的标准库函数.但是我习惯于使用objective-c或点符号.我没有看到这个"cout"函数是什么对象.

我可以更容易地理解printf,因为至少它为参数提供了大括号.例如printf("你的字符串在这里").

c++

11
推荐指数
2
解决办法
6209
查看次数

"_in_"和"_in_opt_"在C++/CLI中意味着什么?

我是CLR的新手,我正在阅读setWindowPos的c ++/CLI文档,并且函数定义如此.

BOOL WINAPI SetWindowPos(
  _In_      HWND hWnd,
  _In_opt_  HWND hWndInsertAfter,
  _In_      int X,
  _In_      int Y,
  _In_      int cx,
  _In_      int cy,
  _In_      UINT uFlags
);
Run Code Online (Sandbox Code Playgroud)

我有c ++的经验,所以我理解,例如,"HWND"是数据类型,"hWnd"是变量名.

但是什么是" _ in _"和"_in_opt_"?

我猜它们是"输入变量"或其他东西的缩写.

文档中提到hWndInsertAfter是可选的.这是否意味着我可以简单地省略/不打扰在我的函数调用中将变量传递给此参数,如果我不需要?

例如

SetWindowPos(this,0,0,GetSystemMetrics(SM_CXMAXIMIZED),GetSystemMetrics(SM_CYMAXIMIZED),SWP_NOZORDER);
//Note that we're one parameter short here (the second is missing)
Run Code Online (Sandbox Code Playgroud)

(这会让我感到困惑,因为我已经看到它在其他地方写的C++不支持可选参数.只有默认参数和重载)

c++-cli

10
推荐指数
1
解决办法
7441
查看次数

枚举和枚举类之间的区别?

我最近开始使用C++/CLI托管代码,但我总是定义这样的枚举:

enum FV_MODE
{
    IDLE,DRAG,ADD_HITBOX,ADD_HURTBOX
};
Run Code Online (Sandbox Code Playgroud)

直到今天,当我遇到错误消息:

cannot define an unmanaged enum 'FViewer::FV_MODE' inside managed 'FViewer'
1>          use 'enum class'
Run Code Online (Sandbox Code Playgroud)

正如消息和各种Stack Overflow问题中所建议的那样,将我的代码更改为:

enum class FV_MODE
{
    IDLE,DRAG,ADD_HITBOX,ADD_HURTBOX
};
Run Code Online (Sandbox Code Playgroud)

迅速解决了问题.

但是,我仍然没有意识到我现在知道定义枚举的两种不同方式之间的差异.有人可以帮我澄清一下吗?还有什么使"枚举类"更适合托管代码?

提前致谢,

家伙

c++ enums c++-cli enum-class

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

什么是删除我的指针?

我常常想知道,

我知道我可以使用new关键字将同一个指针作为参数传递给函数,同时创建一个指向对象实例的指针.就像我在下面Animation::newFrame的例子中给出的功能一样.

但是,我也知道,作为一般规则,我负责调用delete我创建的东西new.

所以当我像这样调用Frame的构造函数时:

Frame* myFrame
    = new Frame(new point(0,0), new point(100,100), new point(50,20));
Run Code Online (Sandbox Code Playgroud)

对于我new在上面的函数调用中创建的3个点最终释放内存的责任在哪里?

毕竟,以上3个新点并没有让我可以打电话的名字delete.

我总是假设它们属于它们被调用的函数的范围,并且它们将简单地超出函数的范围.然而,最近我一直在想也许不是这样.

我希望我在这里已经足够清楚了.

提前致谢,

家伙

struct Frame
    {
    public:
        point f_loc;
        point f_dim;
        point f_anchor;

    //the main constructor:: Creates a frame with some specified values
        Frame(point* loc, point* dim, point* anchor)
        {
            f_loc = loc;
            f_dim = dim;
            f_anchor = anchor;
        }

    };

struct Animation
    {
    public:
        vector<Frame*> frameList;

    //currFrame will always be …
Run Code Online (Sandbox Code Playgroud)

c++ scope memory-management

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

我尝试翻转(镜像)UIImage的方式有什么问题?

我已经尝试了几天了.我正在创建一个sprite表加载器,但是我也必须能够加载面向相反方向的sprite.这涉及翻转我已经加载的图像.

我已经尝试使用UIImageOrientation/UIImageOrientationUpMirrored方法执行此操作,但这绝对没有任何效果,只需绘制与之前完全相同的方向框架.

从那时起,我尝试了一种稍微复杂的方式,我将在下面介绍.但仍然只是以与加载到应用程序中完全相同的方式绘制图像.(没有镜像).

我已经包含了下面的方法(以及我的评论,以便你可以按照我的思维模式),你能看到我做错了吗?

- (UIImage*) getFlippedFrame:(UIImage*) imageToFlip
{
//create a context to draw that shizz into
UIGraphicsBeginImageContext(imageToFlip.size);
CGContextRef currentContext = UIGraphicsGetCurrentContext();



//WHERE YOU LEFT OFF. you're attempting to find a way to flip the image in imagetoflip. and return it as a new UIimage. But no luck so far.
[imageToFlip drawInRect:CGRectMake(0, 0, imageToFlip.size.width, imageToFlip.size.height)];

//take the current context with the old frame drawn in and flip it.
CGContextScaleCTM(currentContext, -1.0, 1.0);

//create a UIImage made from the flipped context. However …
Run Code Online (Sandbox Code Playgroud)

iphone cocoa-touch objective-c ios

5
推荐指数
1
解决办法
1642
查看次数

NUnit 是否在每次测试之间重新实例化标记为 TestFixtures 的类?

我有一个 TestFixture 标记的类,它是对名为“HideCurrentTitleBarMessageTask”的类的功能进行单元测试。

在这个类中,我使用一个替代来模拟 [Setup] 方法中的一个接口,并且在测试类中的一些测试中,我设置了一个来自模拟接口成员的返回结果。

[TestFixture]
public class TestClass_A {

    private ITitleBarMessageModel model;
    private ITitleBarMessage message;
    private HideCurrentTitleBarMessageTask task;
    private bool taskCompleteFired;


    [SetUp]
    public void SetUp() {
        taskCompleteFired = false;
        model = Substitute.For<ITitleBarMessageModel>();
        message = Substitute.For<ITitleBarMessage>();
        //model.currentlyDisplayedMessage = null;
        task = new HideCurrentTitleBarMessageTask();
        task.Init(model);
        task.Completed += (t) => { taskCompleteFired = true; };
    }

    [Test]
    public void Test_A() {
        task.Execute();
        Assert.That(taskCompleteFired, Is.True);
    }

    [Test]
    public void Test_B() {
        model.currentlyDisplayedMessage.Returns(message);
        task.Execute();
        message.Received(1).Hide();
        Assert.That(taskCompleteFired, Is.False);
    }
}
Run Code Online (Sandbox Code Playgroud)

HideCurrentTitleBarMessageTask 的 Execute 函数如下所示 …

c# nunit nsubstitute

5
推荐指数
1
解决办法
805
查看次数

OpenGL渲染纹理全白

我正在尝试将.png图像渲染为纹理.但是,所有渲染的都是白色方块.

我给我的纹理一个名为texID的唯一int ID,将pixeldata读入缓冲区'image'(在.h文件中声明).我加载了我的pixelbuffer,完成了我的所有OpenGL内容并将该pixelbuffer绑定到OpenGL的纹理.然后我使用glDrawElements绘制所有内容.

此外,我在调用其构造函数时初始化大小为32x32的纹理,因此我怀疑它与两个大小问题的能力有关.

任何人都可以看到我的OpenGL GL_TEXTURE_2D设置中的任何错误,可能会给我一个块白色方块.

 #include "Texture.h"




Texture::Texture(int width, int height, string filename)
{

    const char* fnPtr = filename.c_str(); //our image loader accepts a ptr to a char, not a string
    printf(fnPtr);
    w = width; //give our texture a width and height, the reason that we need to pass in the width and height values manually
    h = height;//UPDATE, these MUST be P.O.T.

    unsigned error = lodepng::decode(image,w,h,fnPtr);//lodepng's decode function will load the pixel data into image vector
    //display any …
Run Code Online (Sandbox Code Playgroud)

c++ opengl

4
推荐指数
1
解决办法
8960
查看次数

为什么我得到"不是类或命名空间"错误

我有一个生成错误的文件

"Block" is not a class or a namespace name
Run Code Online (Sandbox Code Playgroud)

在线上

typedef Block::point point;
Run Code Online (Sandbox Code Playgroud)

但是,我很肯定这是我在下面的文件中创建和#included的类

'texture.h'.

    #pragma once
#include "Block.h"
#include <iostream>
#include <vector>
#include <GL/glut.h>
#include "lodepng.h"

using namespace std;
typedef Block::point point;


class Texture
{



public:
    Texture(int width, int height, string filename);//initialises our texture and loads its pixeldata into a buffer
    Texture(void);
    ~Texture(void);

    void draw(point centerPoint, point dimensions);

    unsigned int w;//width of our imagefile
    unsigned int h;

    GLuint texID;//the ID we will give OGL for this particular texture. …
Run Code Online (Sandbox Code Playgroud)

c++ scope

2
推荐指数
1
解决办法
5937
查看次数

将指向枚举的指针传递给函数

我试图将在函数范围之外声明的枚举传递给函数,以便更改函数内枚举的值会更改指向的枚举值.仅仅在对象范围内使用枚举本身不是一个选项,因为我希望将此函数与此枚举的多个不同实例一起使用.

我有一个枚举'ColourState'.宣布如此.和指向ColourState的指针.

enum ColourState {COLOUR1, COLOUR2};
ColourState* CS_GO_score;
Run Code Online (Sandbox Code Playgroud)

指针初始化如此.

CS_GO_score = new ColourState;
*CS_GO_score = ColourState::COLOUR2;
Run Code Online (Sandbox Code Playgroud)

我现在正试图将CSS_GO_score指向的ColourState传递给函数'pulsateColour'

像这样.

void HUD::pulsateColour(GLfloat *colour1, GLfloat *colour2, GLfloat *objectCol, ColourState goingTo, int timeInFrames)
{
    GLfloat difference[4];
    //give us an array of values to change the array by in the alloted time
    difference[0] = (colour1[0]-colour2[0])/timeInFrames;
    difference[1] = (colour1[1]-colour2[1])/timeInFrames;
    difference[2] = (colour1[2]-colour2[2])/timeInFrames;
    difference[3] = (colour1[3]-colour2[3])/timeInFrames;

    //depending on the state, transform the array in one direction or another

    if(goingTo == ColourState::COLOUR2)//if we're moving toward colour 2
    { …
Run Code Online (Sandbox Code Playgroud)

c++ enums pointers

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

VS告诉我ctype.h中存在构建错误

我去编译我的程序的最新版本,我的恐怖VS给了我以下构建错误消息.当我单击错误消息让它带我到问题,它带我到ctype.h头文件?

1>c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\ctype.h(22): error C2143: syntax error : missing ';' before 'string'
1>c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\ctype.h(22): error C2059: syntax error : 'string'
1>c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\ctype.h(22): error C2143: syntax error : missing ';' before '{'
1>c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\ctype.h(22): error C2447: '{' : missing function header (old-style formal list?)
Run Code Online (Sandbox Code Playgroud)

现在我知道我还没有玩过ctype.h.

自上次构建以来我编辑的唯一文件甚至还没有进入主代码的入口点.

我会把它们包括在下面.

BowlingPin.cpp

#include <vector>
#include "BowlingPin.h"
#include "Entity.h"
#include <iostream>
#include <GL/glut.h>
#include "NormalCalculator.h"


vector<GLfloat> pinVerts;
vector<GLfloat> pinNorms;
vector<GLubyte> …
Run Code Online (Sandbox Code Playgroud)

c++ opengl visual-studio-2010

1
推荐指数
1
解决办法
1561
查看次数

简单的"捕捉"逻辑

我有一个字符串^,它被转换为Uint32在下面的代码中:

try
{
    int newX = System::Convert::ToUInt32(this->cbXSizeBox->Text);
}
catch (FormatException^ e)
{
    printf("\nNot a valid 3-digit number");
    this->cbXSizeBox->Text = System::Convert::ToString(capBoxSize->x);
}
Run Code Online (Sandbox Code Playgroud)

这很好.(FYI capBoxSize-> x是另一个可以评估为uint32的值).

基本上捕获的是捕获cbXSizeBox-> Text(这是一个字符串)的值,回到它的默认值,如果用户输入除数字之外的任何内容(例如2g9).

如果catch块没有捕获格式异常,我想添加代码来将capBoxSize-> x的值更改为它的新有效值.我试图找到一些对编译器说的内容,"如果你抓住了这个异常,就这样做.但是如果你没有捕获异常,那就去做吧." 是否可以在if else语句中包含catch块?

如果您了解我正在尝试做什么,任何建议将不胜感激.

PS在try块中更改capBoxSize-> x的代码实际上并不是我认为的选项.因为这可能会尝试将newX分配给capBoxSize-> X这样的"2ty",这是一个Uint32.这可能会导致错误.

c++ exception-handling c++-cli try-catch

1
推荐指数
1
解决办法
96
查看次数

"|"的确切功能是什么?分离参数时的(垂直线)运算符

我用过双垂直"||" 作为布尔"或"运算符.并看到"|" 是按位还是.

但是,由于我已经开始使用c ++/cli,我注意到它曾经用一个似乎接受多个标志的单个参数来分隔函数中的标志.

这方面的一个例子是msdn的MessageBox()函数示例.

int msgboxID = MessageBox(
        NULL,
        (LPCWSTR)L"Resource not available\nDo you want to try again?",
        (LPCWSTR)L"Account Details",
        MB_ICONWARNING | MB_CANCELTRYCONTINUE | MB_DEFBUTTON2
    );
Run Code Online (Sandbox Code Playgroud)

"|"执行的操作到底是什么 这里?

什么是"|" 实际上叫做符号?(就像"^"被称为插入符号,而不是我在编程之前所知道的,这是"倒置V"):D

我问的原因是我正在使用函数setWindowPos(),它也接受标志作为参数.函数声明如下:

BOOL WINAPI SetWindowPos(
  _In_      HWND hWnd,
  _In_opt_  HWND hWndInsertAfter,
  _In_      int X,
  _In_      int Y,
  _In_      int cx,
  _In_      int cy,
  _In_      UINT uFlags
);
Run Code Online (Sandbox Code Playgroud)

我想知道标志是否可以像MessageBox()中那样组合.

提前致谢,

家伙

c++ flags operators

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

为什么我的代码生成"未解析的外部符号"错误?

我一直在编写我的代码并声明并定义了所有必要的变量并包含了必要的头文件.它仍然在类构造函数中使用的任何函数上给出了"未解析的外部符号".

BowlingAlley.obj : error LNK2001: unresolved external symbol "private: void __thiscall     BowlingAlley::addToMasterArray(int * const,int * const)" (?addToMasterArray@BowlingAlley@@AAEXQAH0@Z)
1>BowlingAlley.obj : error LNK2001: unresolved external symbol "private: void __thiscall     BowlingAlley::createLaneArrays(void)" (?createLaneArrays@BowlingAlley@@AAEXXZ)
1>BowlingAlley.obj : error LNK2001: unresolved external symbol "int * laneVertices" (?laneVertices@@3PAHA)
Run Code Online (Sandbox Code Playgroud)

我的头文件如下.

#pragma once

#include <stdio.h>
#include <iostream>
#include <array>



class BowlingAlley
{
private: 
    void createLaneArrays();
    void concatenateArrays(int array1[], int array2[], int containerArray[]);
    int getNewArrayLength(int array1[], int array2[]);
    //mutates an array creating the master draw array by using the two functions …
Run Code Online (Sandbox Code Playgroud)

c++ oop class unresolved-external

0
推荐指数
1
解决办法
1102
查看次数