我想在VS2008下编译beecrypt库.但是以下几种结构会产生语法错误(C2059语法错误:'.'):
const hashFunction md5 = {
.name = "MD5",
.paramsize = sizeof(md5Param),
.blocksize = 64,
.digestsize = 16,
.reset = (hashFunctionReset) md5Reset,
.update = (hashFunctionUpdate) md5Update,
.digest = (hashFunctionDigest) md5Digest
};
Run Code Online (Sandbox Code Playgroud)
VC++不接受开头的点.如果我评论上面的内容,我稍后会收到链接错误(LNK2001未解析的符号_md5) - 所以我想它必须取消注释.
这个结构是什么?我需要什么?我如何告诉VS2008编译它?
我正在将一些狡猾的C++ Windows代码移植到Linux上,它在每个类中都使用了"open"和"close"的功能......非常糟糕的风格,还是?幸运的是,这不是Windows中的问题,因为他们的系统调用被命名为不同.
当我尝试调用systemcalls open()或close()时,我收到一些关于"没有匹配函数调用class:open()"的编译器错误.我不能在整个代码中重命名所有名为"class :: open"和"class :: close"的函数,我必须使用open()和close(),因为我正在使用串行端口.
所以我的问题是:我怎么能告诉编译器,我打开哪个?如何在C++中转义或隐藏类的命名空间?
在我的代码中,我有一个地图,其中包含大量数据(~100MB)我需要将所有数据从一个地图复制到另一个地图.目前我用交换做这个,但根据我的理解,交换是一种花哨的方式来复制.有没有办法简单地转移两张地图使用的内存?我认为我可以用指针做到这一点,但我希望有一个更优雅的方式.
尝试使用类型转换的指针调用析构函数时,我的代码出现分段错误。但是如果我将析构函数更改为非虚拟的,它就可以正常工作。
#include <iostream>
using namespace std;
class Test
{
public:
Test() { cout << "Cons" << endl;}
~Test() {cout << "Des"<<endl;}
void *var_ptr;
};
class Test3
{
public:
Test3() { cout << "Cons3" << endl;}
//virtual ~Test3(){cout << "Des3" << endl;};
~Test3(){cout << "Des3" << endl;};
};
class Test2:public Test3
{
public:
Test2() { cout << "Cons2" << endl;}
~Test2() {cout << "Des2"<<endl;}
};
int main ()
{
Test *testPtr = new Test();
int *ivalue ;
ivalue = new int; …
Run Code Online (Sandbox Code Playgroud) 我在一个返回字符串的类中有一个函数.在这个函数中,我只能cout<<endl
在return语句之前添加函数时才能使它工作 .知道为什么会这样,或者我如何解决它?我在Mac上用Eclipse运行它
在"main.cpp"中:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
#include "Braid.h"
using namespace std;
static int size=3;
int main(){
Braid * b1 = new Braid(size);
b1->setCanon();//creates canonical braid.
cout<<"a ";
cout<<b1->getName()<<endl;
cout<<" b ";
}
Run Code Online (Sandbox Code Playgroud)
在"Braid.h"中:
public:
Braid(int);
void setCanon();
string getName();
};
Run Code Online (Sandbox Code Playgroud)
在"Braid.cpp"中:
string Braid::getName(){
string sName="";
/* body commented out
for(int i=0; i<height; i++)
{
for(int j=2; j<(width-2); j++)
{
sName += boxes[i][j];
sName += "|";
}
}
*/
//cout<<endl;
return sName; …
Run Code Online (Sandbox Code Playgroud) 在STL引用中给出了字符串类在字符串头中,然后不包括头如何运行以下程序而没有错误?
#include<iostream>
using namespace std;
int main() {
string s;
cin>>s;
cout<<"string entered is : "<<s;
}
Run Code Online (Sandbox Code Playgroud)
我在ubuntu机器上使用g ++编译器.
#include<iostream>
#include<vector>
#include<algorithm>
class Integer
{
public:
int m;
Integer(int a):m(a){};
};
class CompareParts
{
public:
bool operator()(const Integer & p1,const Integer & p2)
{
return p1.m<p2.m;
}
}obj1;
int main()
{
std::vector<Integer> vecInteger;
vecInteger.push_back(Integer(12));
vecInteger.push_back(Integer(13));
vecInteger.push_back(Integer(5));
vecInteger.push_back(Integer(7));
vecInteger.push_back(Integer(9));
Integer obj2();
std::sort(vecInteger.begin(),vecInteger.end(),obj1);
std::sort(vecInteger.begin(),vecInteger.end(),obj2);
}
Run Code Online (Sandbox Code Playgroud)
为什么第二排序函数中的obj2会导致编译错误.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <cstring>
void initialize(char[],int*);
void input(const char[] ,int&);
void print ( const char*,const int);
void growOlder (const char [], int* );
bool comparePeople(const char* ,const int*,const char*,const int*);
int main(){
char name1[25];
char name2[25];
int age1;
int age2;
initialize (name1,&age1);
initialize (name2,&age2);
print(name1,age1);
print(name2,age2);
input(name1,age1);
input(name2,age2);
print(name1,age1);
print(name2,age2);
growOlder(name2,&age2);
if(comparePeople(name1,&age1,name2,&age2))
cout<<"Both People have the same name and age "<<endl;
return 0;
}
void input(const char name[],int &age)
{
cout<<"Enter a name :"; …
Run Code Online (Sandbox Code Playgroud) public class Module
{
// required instance variables
private String codeModule, titleModule;
private int pointsModule;
//three-argument constructor receiving a code, a title string, and a
//number of points,
//which sets the code, title and points data of the created object to
//the received values.
public Module(String aCode, String aTitle, int aPoints)
{
codeModule = aCode;
titleModule = aTitle;
pointsModule = aPoints;
}
//set the instance data value codeModule to received argument newCode.
public void setaCode(String newCode)
{
codeModule = newCode; …
Run Code Online (Sandbox Code Playgroud) 我对此有疑问:
class A
{
int a;
int* pa;
public:
A(int i):a(i) , pa(new int(a))
{
cout<<"A ctor"<<a<<endl;
}
~A()
{
delete pa;
cout<<"dtor\n";
}
int * &get()
{
return pa;
}
};
class B : public A
{
int b;
public:
B (A obj): A(obj) , b(0)
{
cout<<"B ctor\n";
}
~B()
{
cout<<"B dtor\n";
}
};
int main()
{
int i = 23 ;
A* p = new B(i);
}
Run Code Online (Sandbox Code Playgroud)
可以告诉我为什么最后一行main
编译?我传递一个int
进入B
的构造,其预期的A
目的,而不是.我相信它 …
在c ++中是否可以在内存中的特定位置分配对象?我正在实现我的业余爱好os内核内存管理器,它提供void*
了存储我的东西的地址,我想知道如何使用该指针在那里分配我的对象.我试过这个:
string* s = (string*)235987532//Whatever address is given.
*s = string("Hello from string\n\0");//My own string class
//This seems to call the strings destructor here even thought I am using methods from s after this in my code...
Run Code Online (Sandbox Code Playgroud)
唯一的问题是它调用字符串对象析构函数,它不应该这样做.任何帮助表示赞赏.
编辑:我不能使用placement new,因为我正在开发内核级别.
c++ ×10
c ×1
c99 ×1
destructor ×1
functor ×1
inheritance ×1
java ×1
namespaces ×1
overloading ×1
overriding ×1
pointers ×1
sorting ×1
stl ×1
string ×1
swap ×1
system-calls ×1