struct LeafDataEntry
{
void *key;
int a;
};
int main(){
//I want to declare a vector of structure
vector<LeafDataEntry> leaves;
for(int i=0; i<100; i++){
leaves[i].key = (void *)malloc(sizeof(unsigned));
//assign some value to leaves[i].key using memcpy
}
}
Run Code Online (Sandbox Code Playgroud)
我在上面的for循环中执行malloc的时候得到了这个代码的SEG FAULT错误....任何建议任何替代方案都可以在结构向量中为指针分配内存.
这段代码安全吗?
int main() {
struct {
int foo;
int bar;
std::list<...>::iterator it;
} foobar;
memset(&foobar, 0, sizeof(foobar));
foobar.it = ...;
}
Run Code Online (Sandbox Code Playgroud)
我认为它是安全的,因为std::iterator似乎没有覆盖operator=.这是一个有效的推理吗?
std::is_base_of<B, D>如果D是从B派生的,或者两者都是相同的非联合类,则C++ 11标准必须返回true.但是在VC++ 2010中,如果类型B是D类型的唯一基类,则stl实现模板返回true!这是一个错误吗?
在http://msdn.microsoft.com/en-us/library/bb982243(v=vs.100).aspx页面的示例部分
std::is_base_of<base, base> == true
Run Code Online (Sandbox Code Playgroud)
但这与VC++ 2010 STL实现不符.虽然它符合C++ 11标准的要求.哪个错误?
我正在努力解决这段代码:
std::queue<char> output_queue;
std::string output_string
// put stuff into output_queue
while (!output_queue.empty())
{
output_string.insert(0,(output_queue.front()));
output_queue.pop();
}
Run Code Online (Sandbox Code Playgroud)
我不知道怎么办不行,因为它std::queue<char>::front()会返回一个char&,我不能把它放进去std::string.
现在我有一个类进行二分查找.该类接受一个向量,但后来我告诉该类进行排序.
我需要能够只通过名字潜在或姓氏排序,所以我设置一个字符参数作为该类中的一个选项来改变我对矢量的排序方式.我也在那个类中创建了一个operator()函数来使用*this,作为一个类指针来对向量进行排序.但它似乎只是永远循环.谁能告诉我为什么?代码如下.
*注意是否有一些我不遵循的一般做法随时通知我.我现在不想开始养成坏习惯.
按要求:Getname
void personType::getName(string& first, string& last)
{
// get the name and set it
first = firstName;
last = lastName;
}
bool sBinary::operator()(studentType student1, studentType student2){
string toCheck1, toCheck2, fName1,lName1 ,fName2 , lName2;
student1.getName(fName1, lName1);
student2.getName(fName2, lName2);
toCheck1=checkStr(fName1, lName1);
toCheck2=checkStr(fName2,lName2);
return toCheck1<toCheck2;
}
string sBinary::checkStr(string fName, string lName){
string toCheck;
switch (choice){
case 'f':
case 'F':
toCheck=fName;
break;
case 'l':
case 'L':
toCheck=lName;
break;
case 'r':
case 'R':
toCheck=fName+lName;
break;
default:
toCheck=lName+fName;
}
return toCheck;
}
sBinary::sBinary(vector<studentType> …Run Code Online (Sandbox Code Playgroud) 我无法想象这次std::all_of电话我做错了什么.
我有一个班级统计:
class Statistics {
public:
bool isDataSet() const { return m_data.size() > 0; }
private:
std::vector<double> m_data;
};
Run Code Online (Sandbox Code Playgroud)
Statistics类的每个实例对应于某个对象.
在另一个不同文件中的函数中,我想仅在数据已在所有 Statistics实例中初始化时才显示统计信息.我想以std::all_of下列方式使用函数:
if( std::all_of(m_stats.begin(), m_stats.end(), &Statistics::isDataSet) ) {
...
}
Run Code Online (Sandbox Code Playgroud)
哪里 std::vector<Statistics*> m_stats.
编译器报告错误,因为'谓词术语不评估为采用1个参数的函数'.据我所知,每个类成员都将此指针作为第一个参数传递,因此Statistics::isDataSet()实际上应该是一个带有1个参数的函数.但是std::all_of看错了.
我的假设是错误的,Statistics::isDataSet()应该被接受为带有1个参数的函数std::all_of()吗?
类型特征很酷,我使用它们,因为几年前它们起源于提升.但是,当你看看它们的实现时(查看"如何is_base_of工作?" StackOverflow线程).
为什么编译器不会在这里帮忙?例如,如果你想检查某个类是否是另一个类的基础,编译器已经知道了,为什么它不能告诉我们?这将使概念更容易实现和使用.你可以在那里使用语言结构.
我不确定,但我猜它会提高一般性能.这就像向编译器寻求帮助,而不是C++语言.
我怀疑主要原因听起来像"我们需要保持向后兼容性"并且我同意,但为什么编译器不会更积极地生成通用模板化代码?
这就是我设置代码的方法:
class SomeClass
{
public:
std::function<void()> someFunction;
}
Run Code Online (Sandbox Code Playgroud)
AnotherClass.h
class AnotherClass
{
public:
void anotherFunction();
void yetAnotherFunction();
SomeClass someClass;
}
Run Code Online (Sandbox Code Playgroud)
AnotherClass.cpp
void AnotherClass::anotherFunction()
{
std::cout << "triggerd" << std::endl;
}
void AnotherClass::yetAnotherFunction()
{
someClass.someFunction = &AnotherClass::anotherFunction; //here
}
Run Code Online (Sandbox Code Playgroud)
这导致编译时错误:error C2664: 'std::_Func_class<_Ret>::_Set' : cannot convert parameter 1 from '_Myimpl *' to 'std::_Func_base<_Rx> *'经过一些研究后我发现这是来自VS2012(源)的错误.所以我将标有"here"的行更改someClass.someFunction = std::mem_fn(&AnotherClass::anotherFunction);为链接中的解决方案.但是,现在这会导致另一个编译时错误:error C2562: 'std::_Callable_obj<_Ty>::_ApplyX' : 'void' function returning a value.因此,根据我在该错误上发现的研究,它意味着有一个声明为void的函数返回一个值.这是我被卡住的地方因为我100%确定我没有任何函数返回一个不应该加的值我在更改标记为"here"的行之前没有得到这个错误.我不确定这是否是VS2012中的另一个错误,但错误确实说:std::_Callable_obj<_Ty>::_ApplyX这显然不是我的功能.
有没有人知道为什么会这样,以及我如何解决它?
谢谢.
从VC10移动到VC11时,以下代码无法编译.
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;
using namespace std::placeholders;
typedef std::function<void(int)> CB;
int main()
{
vector<CB> m_CBs;
int m_LongPressGesture;
for_each(m_CBs.begin(), m_CBs.end(), bind(&CB::operator(), _1, m_LongPressGesture));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误输出是非法间接的.这是完整的输出.
1>------ Build started: Project: CompileError, Configuration: Debug Win32 ------
1>Build started 2/12/2013 4:32:56 PM.
1>InitializeBuildStatus:
1> Touching "Debug\CompileError.unsuccessfulbuild".
1>ClCompile:
1> main.cpp
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional(1269): error C2100: illegal indirection
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional(1152) : see reference to function template instantiation '_Rx std::_Pmf_wrap<_Pmf_t,_Rx,_Farg0,_V0_t,_V1_t,_V2_t,_V3_t,_V4_t,_V5_t,<unnamed-symbol>>::operator ()<std::function<_Fty>>(_Wrapper …Run Code Online (Sandbox Code Playgroud) 我在VS2012 x64快递版上遇到错误.相同的代码在VS2010下正常工作.我在stackoverflow上经历了很多线程,这似乎是VS2012中的一个错误.
代码:
typedef vector< vector<cv::Point2d> > vec_type;
vec_type table;
table.assign( 100, 0 );
Run Code Online (Sandbox Code Playgroud)
错误:
错误C2664:'
void std::vector<_Ty>::assign(unsigned __int64, const std::vector<cv::Point2d> &)':无法将参数2从'int' 转换为'const std::vector<_Ty> &'
任何人都可以指出解决方案或解决这个问题吗?
谢谢