我正在尝试编译我的程序,我正在使用sqrt pow和fabs等功能.我确实包含math.h但由于某种原因我得到的错误如下:
error C2668: 'fabs' : ambiguous call to overloaded function
Run Code Online (Sandbox Code Playgroud)
其他功能相同,包括:
#include "stdafx.h"
#include "math.h"
Run Code Online (Sandbox Code Playgroud)
我试过包括但仍然是同样的错误.有谁知道他们为什么不被承认?我的文件是.cpp而不是.c但它是一个MFC项目.
谢谢
我需要访问一个向量指针元素,我的动画结构有以下代码(这里简化了,不必要的变量被截断):
struct framestruct {
int w,h;
};
struct animstruct {
vector<framestruct> *frames;
};
vector<framestruct> some_animation; // this will be initialized with some frames data elsewhere.
animstruct test; // in this struct we save the pointer to those frames.
void init_anim(){
test.frames = (vector<framestruct> *)&some_animation; // take pointer.
}
void test_anim(){
test.frames[0].w; // error C2039: 'w' : is not a member of 'std::vector<_Ty>'
}
Run Code Online (Sandbox Code Playgroud)
阵列工作,我通过测试它:
test.frames->size()它是我计划的7.
那么如何从向量中的第N个索引处访问向量元素(w和h)?
我的编译器支持C++ 03.我应该使用哪个版本的提升?如果我使用的是早期版本的C++,我将如何建立升级版本?
是否有C++标准类型用于保持具有恒定大小的向量?例如,像所有元素类型相同的元组之类的东西,所以我只需要提供大小作为模板参数?
我希望有与使用时相同/相似的行为std::vector,但类型应该像原始数组一样紧凑和高效(因此没有动态分配,没有运行时大小信息等)
我更喜欢兼容C++ 03的解决方案,所以重用a std::tuple不是我想要的.
以下课程是否符合我的要求?
template<typename T, int N>
struct vec
{
T component[N];
// (+ some logic and accessors like operator[]...)
};
// concrete example:
vec<int,3> myVector;
Run Code Online (Sandbox Code Playgroud)
它真的不同于T myVector[N](具体例子int myVector[3])吗?因为这是我目前正在做的事情,但我遇到了一些问题,因为原始数组只是"指针"(+大小信息),不能用作返回值,也不能真正按值传递(没有深拷贝发生).
我在C++中使用函数数组的经验不多.我需要使用一个函数数组,其中数组包含来自不同对象的函数.这里有一些虚拟代码来说明我想要实现的内容.
class base_class
{
public:
virtual int function1(int arg1, int arg2);
virtual int function2(int arg1, int arg2);
};
class derived_class : public base_class
{
public:
int function1(int arg1, int arg2) { /* ... */ };
int function2(int arg1, int arg2) { /* ... */ };
// ...
};
typedef int (*functions) (int arg1, int arg2);
int main()
{
derived_class object1;
derived_class object2;
functions func_instance[4];
func_instance[0] = object1.function1;
func_instance[1] = object1.function2;
func_instance[2] = object2.function1;
func_instance[3] = object2.function2;
// ...
}
Run Code Online (Sandbox Code Playgroud)
我无法让它工作,它抛出以下错误: …
我有一些遗留函数返回非null终止字符串.
struct legacy {
char a[4]; //not null terminated
char b[20]; //not null terminated
};
Run Code Online (Sandbox Code Playgroud)
我传递了很多这些char数组,我需要一个干净的方法将它们转换为null终止.
截至目前,这就是我在做的事情:
legacy foo;
std::string a(foo.a, sizeof(foo.a));
std::string b(foo.b, sizeof(foo.b));
bar(foo.a.c_str(), foo.b.c_str());
Run Code Online (Sandbox Code Playgroud)
有没有更清洁的方法我可以使用类和模板来减少这些代码...
legacy foo;
bar(make_null_terminated(foo.a), make_null_terminated(foo.b));
Run Code Online (Sandbox Code Playgroud) 有没有办法,只使用C++ 03标准函数获得std::pair成员,即first或second?
在C++ 11中,我可以分别使用std::get<0>或者std::get<1>在这种情况下.
为什么析构函数在行之后被调用mystring = "Hello, there!";
呢?它不像它已经超出范围了.我肯定错过了一些C++的怪癖!
我知道该行调用默认的复制构造函数,是复制构造函数返回后总是调用的析构函数吗?
正如旁注,我正在使用C++ 03,还没有使用C++ 11.
编辑:另请注意,我知道以下程序导致的双重删除.我在这里做实验.只是想提醒你注意.
class MyString
{
private:
char* mystring;
int m_length;
public:
MyString(const char* str="")
{
assert(str);
m_length = strlen(str) + 1;
mystring = new char[m_length];
for (int i = 0; i < m_length; ++i)
mystring[i] = str[i];
mystring[m_length-1] = '\0';
}
~MyString()
{
delete[] mystring;
}
};
int main()
{
MyString mystring("");
mystring = "Hello, there!";
cout << "Destructor not yet called ";
}
Run Code Online (Sandbox Code Playgroud) 假设您有一个经常重复的常见工作流程,但有一些变化:
我正在尝试实现一种机制,可以自动执行任意操作(在C++ 98中).例如,以下内容:
myMutex.acquire();
int a = foo(arg1, arg2, arg3);
myMutex.release();
return a;
Run Code Online (Sandbox Code Playgroud)
可能成为:
return doMutexProtected(myMutex, foo, arg1, arg2, arg3);
Run Code Online (Sandbox Code Playgroud)
或者一些类似的机制.挑战在于如何为任意类型a和任意类型和数量的参数执行此操作.
我有一种感觉,应该有一种方法来使用模板,但我不知道如何实现它.你可以用仿函数做类似的事情,但你必须提前告诉仿函数他们的参数类型 - 我希望有一种方法可以从被调用的原始函数中自动检测它们.这样,如果(当)函数的参数列表发生变化时,除了要调用的参数列表之外,您不必更新任何内容.
这可能吗?
我看到了如何在C ++ 11中执行此操作的示例:
std::unique(v.begin(), v.end(), [](float l, float r)
{
return std::abs(l - r) < 0.01;
});
Run Code Online (Sandbox Code Playgroud)
但是,这对于我在C ++ 03中失败:
error: template argument for 'template<class _FIter, class _BinaryPredicate> _FIter std::unique(_FIter, _FIter, _BinaryPredicate)' uses local type 'CRayTracer::myFunc()::<lambda(float, float)>'
Run Code Online (Sandbox Code Playgroud)
如何在C ++ 03中做到这一点?我认为Lambda可能已经存在,并且函子/函数对象已经存在,对吗?只是寻找一个简单的解决方案,并不需要是可扩展的-它只会在这里使用。
这是无法为我编译的代码示例:
error: template argument for 'template<class _FIter, class _BinaryPredicate> _FIter std::unique(_FIter, _FIter, _BinaryPredicate)' uses local type 'CRayTracer::myFunc()::<lambda(float, float)>'
Run Code Online (Sandbox Code Playgroud)
这是它产生的错误:
testUnique.cpp: In function 'int main()':
testUnique.cpp:21:37: error: no matching function for call to 'unique(std::vector<float>::iterator, std::vector<float>::iterator, main()::approx_equal&)'
21 | std::unique(v.begin(), v.end(),f);
| …Run Code Online (Sandbox Code Playgroud)