我需要将一些代码从FreePascal移植到C.我是一名专业的C开发人员但对Pascal一无所知.大多数代码都可以轻松移植,但是一行代表让我很头疼.究竟应该做什么:
New(newBack);
curBackPtr^ := newBack;
curBackPtr := @(newBack^.next);
Run Code Online (Sandbox Code Playgroud)
让我感到困惑的是,在没有被访问的情况下newBack分配给它curBackPtr并在其之后分配的事实.那么第一个任务是不是多余的,可以安全地删除?或者我在这里遗漏了什么?newBack.nextcurBackPtrcurBackPtr
如何将push_back()转换为C++ std :: vector而不使用默认定义违反const成员的operator =()?
struct Item {
Item(int value)
: _value(value) {
}
const char _value;
}
vector<Item> items;
items.push_back(Item(3));
Run Code Online (Sandbox Code Playgroud)
我想保留_value const,因为它在构造对象后不应该改变,所以问题是如何在不调用operator =()的情况下用元素初始化我的向量?
这是g ++ v3.4.6给我的基本错误:
.../3.4.6/bits/vector.tcc: In member function `Item& Item::operator=(const Item&)':
.../3.4.6/bits/vector.tcc:238: instantiated from `void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = Item, _Alloc = std::allocator<Item>]'
.../3.4.6/bits/stl_vector.h:564: instantiated from `void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = Item, _Alloc = std::allocator<Item>]'
item.cpp:170: instantiated from here
.../3.4.6/bits/vector.tcc:238: error: non-static const member `const char Item::_value', can't …Run Code Online (Sandbox Code Playgroud) 有没有办法定义如何使用Resharper(6)"清理"注释?我没有找到解决这个问题的方法.
在代码清理之前:
/// <summary>
/// This is a comment.
/// </summary>
public class MyClass
{
... <not yet cleaned code is here>
}
Run Code Online (Sandbox Code Playgroud)
代码清理后:
/// <summary>
/// This is a comment.
/// </summary>
public class MyClass
{
... <cleaned code is here>
}
Run Code Online (Sandbox Code Playgroud)
通缉结果:
/// <summary>
/// This is a comment.
/// </summary>
public class MyClass
{
... <cleaned code is here>
}
Run Code Online (Sandbox Code Playgroud)
备注:我不想为单个类等禁用清理.我只想更改意外添加的注释中的缩进.
我必须int&在头文件中的现有函数定义中添加一个默认参数(例如iNewArgument):
初始化最后一个参数以使其采用默认值的语法是什么?
..
virtual int ExecNew
(int& param1, int& RowIn,int& RowOut, char* Msg = '0',
bool bDebug=true, int& iNewArgument = NULL ) = 0 ;
.
.
Run Code Online (Sandbox Code Playgroud)
NULL为#defined为0
错误:默认参数:无法转换
int为int&
我有以下if语句,其中两个似乎不起作用.当我尝试将它与单个字符"y"或"n"进行比较时,我不明白为什么它有用,但当我尝试将它与另一个if语句中的两个字符进行比较时,我不明白.
我的最后一个问题是,如果有更好的清洁方式来写这个或者这是否可以接受简单的提示检查?
getline(cin,somestr);
if(somestr.empty()){
//do this
}
else if (somestr == "y" || "Y"){
//do something else
}
else if (somestr == "n" || "N"){
//do something else
}
else{}
Run Code Online (Sandbox Code Playgroud) 所以我正在创建一个检测两个精灵之间碰撞的函数,并希望添加两个缓冲区作为可选参数,以填充每个对象碰撞的边的角度.
但是这意味着两个buff参数必须是引用,并且无论何时调用函数都必须存在,并且我不知道有什么方法可以创建默认引用.我怎样才能做到这一点?
这是实际的功能:
bool CheckCollision(T* obj1, T* obj2, float& CollBuff1= new float, float& CollBuff2= new float);
Run Code Online (Sandbox Code Playgroud)
我尝试使用'new'进行默认设置,但它不起作用.
这是一个小程序:
#include <iostream>
using namespace std;
int main() {
f();
system("pause");
}
void f() {
static int x = 20 ;
class tester {
public :
tester() {
cout << x ;
}
} x1;
}
Run Code Online (Sandbox Code Playgroud)
我在这里得到的错误是:错误C3861:'f':找不到标识符
如果我将函数f置于main之上,我将获得所需的输出.
为什么会这样?
我被告知程序执行开始于main.据此,代码也应该在第一种情况下运行.
编译器如何开始阅读程序?
所以我刚接受了面试,他们给了我一份他们希望我事先处理的任务清单.我完成了除了一个以外的所有任务,并且想知道是否有人能够对这个问题有所了解.
问题就是这样的
我完成了任务1-3,但是在问题4上被困了.我在过去的继承工作经历中并没有做太多的事情,这个问题对我来说似乎有点模糊.我向雇主解释了这一点,他们看起来还不错,但没有告诉我应该怎么做.它现在一直困扰着我,我想知道它是如何完成的.
我试图在FLOAT课堂上重载(*,+, - ,/,=)opertors .我写了这堂课:
class FLOAT{
private:
float x;
public:
FLOAT(){ x=0.0; }
void setFloat(float f) { x=f; }
void operator+(FLOAT obj) {x=x+obj.x; };
void operator-(FLOAT obj) {x=x-obj.x; };
void operator*(FLOAT obj) {x=x*obj.x; };
void operator/(FLOAT obj) {x=x/obj.x; };
FLOAT& operator=(const FLOAT& obj) {this->x=obj.x; return *this; };
};
Run Code Online (Sandbox Code Playgroud)
我用它如下:
int main() {
FLOAT f,f2,f3;
f.setFloat(4);
f2.setFloat(5);
f3=f+f2;// here is the problem!
system("pause");//to pause console screen
return 0;
}
Run Code Online (Sandbox Code Playgroud)
f3=f+f2似乎不对.我能做什么?
我使用的priority_queue<unsigned long,vector<unsigned long>,greater<unsigned long> >是C++,内存限制为16MB.我的程序只需要10MB,但一旦达到8653464字节,它会尝试将其容量加倍并抛出一个bad_alloc.
有没有办法使用我当前的实现来阻止这个?可以在priority_queue具有数(n)时间仍然如果我切换到deque[从vector]
c++ ×7
stdvector ×2
.net ×1
c# ×1
const ×1
default ×1
freepascal ×1
gcc ×1
hierarchy ×1
inheritance ×1
pascal ×1
reference ×1
resharper ×1
stl ×1
visual-c++ ×1