【问题标题】:C++ request for member (blank) in (blank) , which is of non-class type (blank) when calling method调用方法时对 (blank) 中的成员 (blank) 的 C++ 请求,该成员是非类类型 (blank)
【发布时间】:2018-02-01 17:14:33
【问题描述】:

我正在尝试使用此页面了解人工神经网络(它在处理中,但我在更改一些小部分时将其转换为 C++):http://natureofcode.com/book/chapter-10-neural-networks/ 但是当我在下面运行代码时,我得到了这个错误:

main.cpp: In function ‘int main()’:
main.cpp:36:7: error: request for member ‘feedforward’ in ‘idk’, which is of non-class type ‘Perceptron()’
  idk->feedforward({1.0, .5});

我环顾四周,但我认为我找不到在调用方法时遇到此错误的人。 代码:

#include <stdio.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <vector>

const double e = 2.71828182845904523536;

float S(float in){
    return 1 / (1 + pow(e, -(in)));
}

double fRand(double fMin, double fMax){
    double f = (double)rand() / RAND_MAX;
    return fMin + f * (fMax - fMin);
}

struct Perceptron{ 
    Perceptron(int n);
    std::vector<double> weights;
    int feedforward(float inputs[]);
};

Perceptron::Perceptron (int n){
    weights.assign(n, fRand(-1.0, 1.0));
}

int Perceptron::feedforward(float inputs[]){
    return 0; // I have this just for testing that I can call it
}

int main(){
    srand(time(NULL));

    Perceptron idk();
    idk.feedforward({1.0, .5});

    return 0;
}

【问题讨论】:

  • 最头疼的解析又来了! :P
  • @Rakete1111 如果其中有更深层次的智慧,那是我无法理解的。你能详细说明一下吗?
  • Perceptron idk(); 不是一个名为idk 的对象的声明;它是不带参数并返回Perceptron 的函数的函数原型。删除括号以解决此问题。
  • 不要破坏你自己的帖子来删除错误的来源。它使问题难以理解。

标签: c++ function methods struct


【解决方案1】:

Perceptron idk(); 是函数的声明,而不是对象。将构造函数参数传递给idk 或创建一个不带参数的默认构造函数。从您的代码看来,您打算将 Perceptron 与默认 ctor 一起使用,因此您可能应该从 idk 声明中删除 () 以使其成为对象而不是函数的声明并删除 int n来自Perceptron 构造函数。

【讨论】:

  • 这个常见问题的名称是“最令人头疼的解析”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-22
  • 2013-11-18
  • 2019-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多