标签: function-definition

从另一个类访问变量(在 C++ 中)

这可能是一个非常简单的问题,但是......就这样吧。(提前致谢!)

我正在简化代码,所以它是可以理解的。我想使用在另一个类中计算的变量而无需再次运行所有内容。

源.cc

#include <iostream>
#include "begin.h"
#include "calculation.h"
using namespace std;
int main()
{
    beginclass BEGINOBJECT;

    BEGINOBJECT.collectdata();
    cout << "class " << BEGINOBJECT.test;

    calculationclass SHOWRESULT;
    SHOWRESULT.multiply();

    system("pause");
    exit(1);
}
Run Code Online (Sandbox Code Playgroud)

开始.h

#include <iostream>
using namespace std;

#ifndef BEGIN_H
#define BEGIN_H

class beginclass
{
    public:
        void collectdata();
        int test;
};

#endif
Run Code Online (Sandbox Code Playgroud)

开始.cpp

#include <iostream>
#include "begin.h"

void beginclass::collectdata()
{
    test = 6;
}
Run Code Online (Sandbox Code Playgroud)

计算.h

#include <iostream>
#include "begin.h"

#ifndef CALCULATION_H
#define CALCULATION_H

class calculationclass
{
    public:
        void multiply();
};

#endif
Run Code Online (Sandbox Code Playgroud)

计算.cpp …

c++ inheritance class function-definition

0
推荐指数
1
解决办法
8811
查看次数

为什么取一个被声明的函数的地址起作用?

我在这里问了一个问题,即取一个函数的地址是否强制编译所述函数,特别是关于Substitution-Failure-Is-Not-An-Error.最直接的答案可以在这里找到:

非正式地,如果一个对象的地址被占用,或者一个引用被绑定到它上面,则该对象被使用,并且如果对它进行函数调用或者取得其地址,则该函数被使用.如果一个对象或函数使用了odr,它的定义必须存在于程序的某个地方; 违反这一点的是链接时错误.

但是我测试的所有编译器都表明这是完全可行的:

void foo(int);
auto bar = &foo;
Run Code Online (Sandbox Code Playgroud)

Live Example

这不合法吗?但如果没有,为什么要建造?

c++ function-pointers addressof function-declaration function-definition

0
推荐指数
1
解决办法
103
查看次数

预期标识符(在 C 中)

我目前正在做计算机科学的 CS50 介绍:我开始编写这段代码(用 C 编写),我必须根据用户编写的内容编写金字塔。

这是我的代码:

#include <stdio.h>

#include <cs50.h>

int main(void);

int n;

do

{

    n = get_int("Positive Number: \n");

}

while (n > 0 || n < 9);
Run Code Online (Sandbox Code Playgroud)

这是我的终端显示的错误:

mario.c:6:1: error: expected identifier or '('
do
^
mario.c:10:1: error: expected identifier or '('
while (n > 0 || n < 9);
^
2 errors generated.
<builtin>: recipe for target 'mario' failed
make: *** [mario] Error 1
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?威廉

c program-entry-point do-while function-definition cs50

0
推荐指数
1
解决办法
109
查看次数

Typescript 中通过方法名称获取类方法的返回类型

假设我们有一堂课:

class Foo {
  var1: string = 'var1';
  var2: string = 'var2';

  hello(request: A): Promise<B> {  }

  world(request: C): Promise<D> {  }
}
Run Code Online (Sandbox Code Playgroud)

我想实现执行以下实例方法的函数Foo

const foo = new Foo();
const executeFoo = (methodName: string, firstParam: any) => { // <- I'm stuck in this arrow function.
  return foo[methodName](firstParam);
};

executeFoo('hello', testParam); // testParams is type of A, then return type should Promise<B>.
executeFoo('world', testParam2); // testParams2 is type of C, then return type should Promise<D>.
Run Code Online (Sandbox Code Playgroud)

有没有办法定义 的类型executeFoo …

javascript higher-order-functions typescript function-definition

0
推荐指数
1
解决办法
2295
查看次数

C 中的 int 链表抛出分段错误

插入到我的链接列表中会导致分段错误,我不确定为什么。这是一个 int 链表。它有时有效,有时则无效。我看不出哪里会出错?也许这与我创建新链表的时间有关。由于链接列表插入最后一个方法在文件读取期间起作用,但在我创建新列表时不起作用。

LinkedList* createLinkedList()
{
    LinkedList* list;

    list = (LinkedList*)malloc(sizeof(LinkedList));
    list->head = NULL;
    list->tail = NULL;
    list->length = 0;

    #ifdef DEBUG
    printf("Linked list. Creation of list complete. Checking list length. %d \n", list->length);
    #endif

    return list;
}

void insertLast(LinkedList* list, int entry)
{
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = entry;

    /*If list is empty*/
    if(list->length == 0)
    {
        list->head = newNode;
        list->tail = newNode;
    }
    else
    {
        newNode->prev = list->tail;
        /*if this is the second item in the list*/ …
Run Code Online (Sandbox Code Playgroud)

c struct linked-list doubly-linked-list function-definition

0
推荐指数
1
解决办法
199
查看次数

为什么这段 C 代码没有为我计算数组中的元素数量?

无法计算数组中元素的数量

#include <stdio.h>

int counter(int arr[])
{
    int c =0;
    for (int i = 0; arr[i] != '\0'; i++)
    {
        c = c + 1;
    }
    return c;
}

int main()
{
    int a[] = { 1, 5, 3 };
    int d = counter(a);
    printf("%d ", d);

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

任何人都可以帮助我如何获得 3 作为其输出吗?

c arrays for-loop sizeof function-definition

0
推荐指数
1
解决办法
129
查看次数

如何访问链表的空指针(节点)?

此方法应该在链接列表的末尾附加一个节点。该方法循环直到到达末尾,即空指针。但是当我尝试将空指针更改为一个值时,它崩溃了。我应该如何解决这个问题?(节点指针有一个整数数据和另一个当前节点指向的节点变量)。

void appendItem(LinkedList* list, int value)
{
    Node* temp = (Node*)malloc(sizeof(Node));
    temp = list->head;

    while(temp != NULL)
    {
        temp = temp->next;
    }

    temp->data = value;
    temp->next = NULL;
}
Run Code Online (Sandbox Code Playgroud)

c linked-list append singly-linked-list function-definition

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

使用指向指针的指针访问结构成员

我有一个如下所示的程序,当我尝试使用指向指针的指针访问结构成员时,它表示表达式必须具有指向类类型的指针。请告诉我如何使用指向指针的指针访问结构对象的数据元素

#include "stdafx.h"
#include<iostream>
struct Node{
    int data;
    Node* next;
};

void addnode(Node** node, int val)
{
    if (node == NULL){
        *node = new Node();
        *node->data = val;

    }
}



int _tmain(int argc, _TCHAR* argv[])
{
    Node* node;
    addnode(&node, 10);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ struct linked-list singly-linked-list function-definition

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