标签: reference

C参考.错误

我坚持使用我的代码20分钟.

这个简单的C代码有什么问题?

void function (char & reference_to_something) {} 
Run Code Online (Sandbox Code Playgroud)

错误:

expected ';' , ',' or ')' before '&' token
Run Code Online (Sandbox Code Playgroud)

c reference

-1
推荐指数
1
解决办法
50
查看次数

如何使用'this'作为另一个对象的参数?

首先我知道这一定很简单,我一直在尝试在Stackoverflow.com和Google上阅读类似的问题,但我仍然无法让我的程序运行.

我很快写了一篇关于我的程序试图做的摘要:

    public class One{

    public One(){
        Two t = new Two(this);
    }

    public void doSomething(){
        sout("HERE");
    }

    public static void main(String[] args){
        One o = new One()
        Two t = new Two(o);
    }

}

    public class Two{

        public Two(One o){
            One o = o;
            o.doSomething();
        }
    }
Run Code Online (Sandbox Code Playgroud)

当我运行程序时,我从方法调用o.doSomething()得到一个NullPointerException.我已经尝试了一段时间来解决它,但我很难过.如果有人能够快速告诉我修复它会非常感激.

java reference this parameter-passing

-1
推荐指数
1
解决办法
94
查看次数

如何在不激怒编译器的情况下将指针传递给结构?

我正在尝试使用MinGW 4.7.2编译一个简单的C++程序,但是由于大量的错误和警告而感到沮丧.

/*\ program.h \*/
typedef struct
{   int member0;
    int member1;
    int member2;
    int member3;
} structure;

void function(&structure);
Run Code Online (Sandbox Code Playgroud)
/*\ program.cpp \*/
#include "program.h"

int main(void)
{   structure *instance;
    function(instance);
}

void function(&structure)
{   // nothing
}
Run Code Online (Sandbox Code Playgroud)

function取一个地址structure.指针instance包含structure随后传递给的地址function.

很简单,但编译器并不开心.

g++ program.cpp
In file included from program.cpp:1:0:
program.h:8:14: error: variable or field 'function' declared void
program.h:8:25: error: expected primary-expression before ')' token
program.cpp: In function 'int main()':
program.cpp:5:19: error: 'function' was …
Run Code Online (Sandbox Code Playgroud)

c++ struct mingw structure reference

-1
推荐指数
1
解决办法
175
查看次数

指针和'char buffer [128]'的引用

我有一个简单的程序:

char buffer[128];                      // creates an array of 128 elements
memset(&buffer, 65, sizeof(buffer));   // fills buffer with 'A' (ascii 65) letter
cout << buffer << endl;                // prints whole buffer - 128 times 'A' letter
cout << &buffer << endl;               // prints adress of a buffer (first element of it)
cout << buffer[0] << endl;             // prints first element of a buffer
Run Code Online (Sandbox Code Playgroud)

直到现在一切都很好,但我不明白为什么,当我这称呼:

cout << &buffer[0] << endl;          
Run Code Online (Sandbox Code Playgroud)

imho上面的代码应该打印缓冲区中第一个元素的地址(&buffer与之相同),但它会打印整个缓冲区,就像cout << buffer << endl;

为什么会这样?

c++ pointers reference

-1
推荐指数
1
解决办法
850
查看次数

java中的引用变量

什么是java中引用变量的默认值(比如它对于原始int等是0)而且当(或如何)是指定NO VALUE的引用变量时,它何时被赋值为THE VALUE"NULL"

java default reference

-1
推荐指数
1
解决办法
223
查看次数

重新分配更改的指针引用

我在使用fileContent变量的代码中遇到了麻烦.我希望fileReader重新分配所做的更改在我的主要工作正常,但它不起作用..

void fileReader(char *fileName, char *fileContent){
    FILE *inputFile = fopen(fileName, "r");

    int fileLength = 0;
    int endFlag = fgetc(inputFile);

    while(endFlag != EOF){
        fileContent = (char *) realloc (fileContent, (fileLength + 1) * sizeof(char));
        fileContent[fileLength] = endFlag;
        endFlag = fgetc(inputFile);

        fileLength++;
    }
}

int main(int argc, char const *argv[]){
    char *fileName = (char *) malloc (sizeof(char));
    char *taskStack = (char *) malloc (sizeof(char));
    char *fileContent = NULL;

    inputReader(fileName, taskStack);
    fileReader(fileName, fileContent);


    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c pointers reference

-1
推荐指数
1
解决办法
84
查看次数

使用参考更新智能指针

我想从参考更新智能指针。

shared_ptr<My_Toy> my_toy_ptr;

// Something...

void update(shared_ptr<My_Toy> my_toy_ptr, My_Toy& toy){

     my_toy_ptr = &toy;
}
Run Code Online (Sandbox Code Playgroud)

...但是他的代码产生了错误。

我该怎么做?

c++ reference smart-pointers shared-ptr c++11

-1
推荐指数
1
解决办法
352
查看次数

为什么我不能以这种方式改变列表?

我想要一个很好的set all list elements to a default value功能.我提出的那个不起作用,但我的第二个解决方案确实如此.任何人都可以告诉我它为什么单向工作而不是另一种方式?

第一解决方案

def set_all(the_list,value): #NOT doing anything
    for item in the_list:
        item = value
Run Code Online (Sandbox Code Playgroud)

二解决方案:

def set_all(the_list,value): #working as intended
    for i,item in enumerate(the_list):
        the_list[i] = value
Run Code Online (Sandbox Code Playgroud)

python reference function list

-1
推荐指数
1
解决办法
61
查看次数

如何将变量传递的指针更改为函数

有人可以告诉我如何实现以下目标.Java不是我的强项,在尝试搜索之后我一直在javas上获取页面总是通过引用传递.

void edit(int[] a){
    a = new String[]{"q","r","s","t","u"};
}

int[] x = new int[]{"a","b","c"};
edit(x);
System.out.println(x); // ["q","r","s","t","u"]    
Run Code Online (Sandbox Code Playgroud)

我试图实现上述目标,以便我可以在将其传递给函数后更改x的指针.

感谢你的帮助

java reference

-1
推荐指数
1
解决办法
52
查看次数

在python中分配值,结果与预期不符

我有这个代码:

x = 'x'
y = []
y.append(x)
z = y
z.append('a')
x = 'X'

print "x:", x
print "y:", y
print "z:", z
Run Code Online (Sandbox Code Playgroud)

输出:

x: X
y: ['x', 'a']
z: ['x', 'a']
Run Code Online (Sandbox Code Playgroud)

我知道这是正确的输出,但我很难理解它产生的原因

y: ['x', 'a']
Run Code Online (Sandbox Code Playgroud)

代替

y: ['x']
Run Code Online (Sandbox Code Playgroud)

python variables reference

-1
推荐指数
1
解决办法
92
查看次数