我的代码非常类似于此:
LINT_rep::Iterator::difference_type LINT_rep::Iterator::operator+(const Iterator& right)const
{
return (this + &right);//IN THIS PLACE I'M GETTING AN ERROR
}
LINT_rep::Iterator::difference_type LINT_rep::Iterator::operator-(const Iterator& right)const
{//substracts one iterator from another
return (this - &right);//HERE EVERYTHING IS FINE
}
err msg: Error 1 error C2110: '+' : cannot add two pointers
Run Code Online (Sandbox Code Playgroud)
为什么我只在一个地方而不是两个地方都收到错误?
有没有什么方法可以对大型数组进行malloc,但是用2D语法引用它?我想要的东西:
int *memory = (int *)malloc(sizeof(int)*400*200);
int MAGICVAR = ...;
MAGICVAR[20][10] = 3; //sets the (200*20 + 10)th element
Run Code Online (Sandbox Code Playgroud)
#define INDX(a,b) (a*200+b);
Run Code Online (Sandbox Code Playgroud)
然后参考我的blob:
memory[INDX(a,b)];
Run Code Online (Sandbox Code Playgroud)
我更喜欢:
memory[a][b];
Run Code Online (Sandbox Code Playgroud)
int *MAGICVAR[][200] = memory;
Run Code Online (Sandbox Code Playgroud)
没有这样的语法吗?请注意我不仅使用固定宽度数组的原因是它太大而无法放在堆栈上.
void toldyou(char MAGICVAR[][286][5]) {
//use MAGICVAR
}
//from another function:
char *memory = (char *)malloc(sizeof(char)*1820*286*5);
fool(memory);
Run Code Online (Sandbox Code Playgroud)
我收到警告,passing arg 1 of toldyou from incompatible pointer type但代码有效,我已经确认访问了相同的位置.没有使用其他功能有没有办法做到这一点?
为什么我不能这样做:
char* p = new char[10];
void SetString(char * const str)
{
p = str;
}
SetString("Hello");
Run Code Online (Sandbox Code Playgroud)
我有一个指向char的const指针,为什么我不能将const指针指向另一个指针?
这似乎是不合逻辑的,因为通过将其分配给另一个指针,您基本上不会违反char指针的常量.或者是你?
编辑:当我编译它时,它说"错误C2440:'=':无法从'char*const*__ w64'转换为'char*'"
(我试图从我正在阅读的书中理解一个概念.只是无法获得编译的代码.
码:
int _tmain(int argc, _TCHAR* argv[])
{
MyString *strg = new MyString(10);
strg->SetString("Hello, ");
MyString *secondstr = new MyString(7);
secondstr->SetString("Tony");
strg->concat(*secondstr, *strg);
}
Run Code Online (Sandbox Code Playgroud)
CPP文件:
#include "MyStringClass.h"
#include <string.h>
#include "stdafx.h"
#include "MyStringClass.h"
void MyString::concat(MyString& a, MyString& b)
{
len = a.len + b.len;
s = new char[len + 1];
strcpy(s, a.s);
strcat(s, b.s);
delete [] s;
}
void …Run Code Online (Sandbox Code Playgroud) 这两种指针有什么区别?据我所知,QSharedPointer可以很好地处理情况,那么QSharedDataPointer需要什么?
我是C/C++的新手,我一直在讨厌,但仍然不知道如何制作这样的"结构"

它应该是一个使用指针的3D动态数组.
我这样开始,但卡在那里
int x=5,y=4,z=3;
int ***sec=new int **[x];
Run Code Online (Sandbox Code Playgroud)
知道如何使其成为y和z的静态大小就足够了;
拜托,我很感激你的帮助.
提前致谢.
int main (int argc, **argv)
{
if (argv[1] == "-hello")
printf("True\n");
else
printf("False\n");
}
Run Code Online (Sandbox Code Playgroud)
# ./myProg -hello False
为什么?我意识到strcmp(argv[1], "-hello") == 0返回true ...但为什么我不能使用相等运算符来比较两个C字符串?
我在看一些c ++代码,我看到了这个:
int num = *(int *)number;
Run Code Online (Sandbox Code Playgroud)
我以前从未见过这个?它是在一个标记为这样的函数中:
void *customer(void *number){ }
Run Code Online (Sandbox Code Playgroud)
这甚至做了什么?是否有不同的方式来展示这个?
谢谢,这不是家庭作业btw我只是困惑这是什么?
我已经看到这个操作符在"在Mac上学习Objective C"的示例代码中弹出了很多次.
我相信它是Objective C继承的C语言的运算符.我尝试使用谷歌搜索和搜索Stack Overflow,奇怪的是没有出现.
它有英文名字吗?
可能重复:
C字符串文字:它们去哪里了?
我所知道的,
通常,指针必须由malloc()分配,并将分配给堆,然后由free()取消分配;
和
非指针(int,char,float等)将自动分配给堆栈,并且只要函数返回就不会分配
但是,从以下代码:
#include <stdio.h>
int main()
{
char *a;
a = "tesaja";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
将a分配到哪里?堆栈还是堆?
我昨天碰到了一个问题,我最终将其提炼成以下最小的例子.
#include <iostream>
#include <functional>
int main()
{
int i=0, j=0;
std::cout
<< (&i == &j)
<< std::less<int *>()(&i, &j)
<< std::less<int *>()(&j, &i)
<< std::endl;
}
Run Code Online (Sandbox Code Playgroud)
当使用启用了优化的MSVC 9.0进行编译时,此特定程序将输出000.这意味着
std::less两个指针都没有按照另一个指令排序,这意味着两个指针根据强加的总顺序相等std::less.这种行为是否正确?std::less不需要与平等运算符一致的总顺序是什么?
是否允许输出以下程序1?
#include <iostream>
#include <set>
int main()
{
int i=0, j=0;
std::set<int *> s;
s.insert(&i);
s.insert(&j);
std::cout << s.size() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)