我有一个库,需要一个数组并填充它.我想使用std :: vector而不是使用数组.而不是
int array[256];
object->getArray(array);
Run Code Online (Sandbox Code Playgroud)
我想要做:
std::vector<int> array;
object->getArray(array);
Run Code Online (Sandbox Code Playgroud)
但我找不到办法做到这一点.有没有机会使用std :: vector?
谢谢阅读!
编辑:我想更新这个问题:我正在玩C++ 11并找到了更好的方法.新的解决方案是使用函数std :: vector.data()来获取指向第一个元素的指针.所以我们可以做到以下几点:
std::vector<int> theVec;
object->getArray(theVec.data()); //theVec.data() will pass the pointer to the first element
Run Code Online (Sandbox Code Playgroud)
如果我们想要使用具有固定数量元素的向量,我们最好使用新数据类型std :: array(btw,因此不应再使用上面问题中使用的变量名"array"! !).
std::array<int, 10> arr; //an array of 10 integer elements
arr.assign(1); //set value '1' for every element
object->getArray(arr.data());
Run Code Online (Sandbox Code Playgroud)
这两种代码变体都可以在Visual C++ 2010中正常工作.请记住:这是C++ 11代码,因此您需要一个支持这些功能的编译器!
如果你不使用C++ 11,下面的答案仍然有效!
我在创建具有不同对象类型的某种形式的层次结构时遇到问题.我有一个类,其中包含另一个类的成员,如下所示:
class A
{
public:
A(){}
~A(){}
void addB(B* dep){
child = dep;
dep->addOwner(this);
}
void updateChild(){
child->printOwner();
}
void print(){
printf("Printing...");
}
private:
B* child;
};
Run Code Online (Sandbox Code Playgroud)
这是B级:
class B
{
public:
void addOwner(A* owner){
ownerObject = owner;
}
//ISNT WORKING
void printOwner(){
ownerObject->print();
}
private:
A* ownerObject;
};
Run Code Online (Sandbox Code Playgroud)
在类"A"之外调用"B"的函数可以正常工作但反之亦然会产生编译器错误,因为A未在B中定义.它实际上是通过使用包含和前向声明,但我想它是一个编译器无法解决的交叉引用问题.
有没有机会解决这个问题,还是我应该重新考虑我的设计?
嗨大家这应该是一个简单的任务,但由于某种原因,我没有得到它...
我只想使用着色器获取场景深度的可视化:
float4x4 matViewProjection;
float4x4 matWorld;
float4 eyePos;
struct VS_OUTPUT
{
float4 position : POSITION0;
float depth : TEXCOORD0;
};
VS_OUTPUT vs_main( VS_INPUT input )
{
VS_OUTPUT output;
output.position = mul( input.position, matViewProjection );
float3 posWorld = mul(input.position, matWorld);
output.depth = distance(posWorld, eyePos);
return( output );
}
Run Code Online (Sandbox Code Playgroud)
为了获得深度值(或者我认为),我计算了世界空间中的位置与视图位置之间的距离.
和相应的像素着色器
float4 ps_main(VS_OUTPUT input) : COLOR0
{
float depth = input.depth;
return float4(depth, depth, depth, 1.0f);
}
Run Code Online (Sandbox Code Playgroud)
除了白色之外什么也没有结果
所以我开始尝试将错误值乘以深度:
float depth = input.depth * 0.005f;
Run Code Online (Sandbox Code Playgroud)
根据到物体的距离给出令人满意的结果.因此,如果我靠近对象,我将不得不再次调整该值.
所以这是非常错误的......
谢谢阅读!
我正在使用-Function绘制到WinAPI窗口SetPixel().
如果我缩放窗口或失去焦点(另一个窗口在顶部),我会丢失我在窗口中绘制的所有内容.
我刚刚用过
RECT rc;
GetClientRect(hwnd, &rc);
RedrawWindow(hwnd, &rc, NULL, RDW_NOERASE | RDW_NOFRAME | RDW_VALIDATE);
Run Code Online (Sandbox Code Playgroud)
这有助于避免在我移动窗口时重新绘制内容,但缩放和失去焦点仍会删除内容.有没有人知道我错过了什么?