c++ - 错误 : ' ' was not declared in this scope

标签 c++ scope

<分区>

所以我正在制作一个简单的几何程序,并进行测试编译。

由于某种原因,当我编译这段代码时,出现以下错误:

base.cc: In member function ‘void seg::init_seg(p, p)’:
base.cc:20:3: error: ‘mid’ was not declared in this scope
base.cc:22:3: error: ‘b’ was not declared in this scope

但有趣的是,点 1 和点 2 没有出现错误,只有 mid 和 b。

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

struct p{
    float x=0.0f,y=0.0f;
    void init_p(float sx, float sy){
        x = sx;
        y = sy;
        }
    };

struct seg{
    p 1, 2, mid, b;
    float length = 0.0f, m = 0.0f;
    void init_seg(p p1, p p2){
        1.init_p(p1.x, p1.y);
        2.init_p(p2.x, p2.y);
        length = sqrt((1.x - 2.x)^2 + (1.y - 2.y)^2);
        mid.init_p((1.x + 2.x)/2, (1.y + 2.y)/2 );
        m = ((1.y - 2.y)/(1.x - 2.x));
        b.init_p(0, (1.y - (m*1.x)));
        }
};

为什么会出现这个错误,为什么只针对这两点?

最佳答案

这是一组错误:

float x=0.0f,y=0.0f;
float length = 0.0f, m = 0.0f;

与 Java 和 C# 不同,您不能像在 C++11 之前的 C++ 中那样进行初始化。在你的情况下,这也是不必要的:你唯一的构造函数同时设置了 xy,所以你设置的零无论如何都会被覆盖。

这是另一个错误:

p 1, 2, mid, b;

您不能使用不以字母或下划线开头的标识符。应该是这样

p p1, p2, mid, b;

关于c++ - 错误 : ' ' was not declared in this scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13409116/

相关文章:

java - jni 调用以自定义 java 接口(interface)作为参数的 java 方法

c# - 将 C# 委托(delegate)注册到 C++ 回调,Marshal.GetFunctionPointerForDelegate 有什么作用?

android - Dagger 2 混合瞄准镜

javascript - 如何/可以在另一个函数内部调用函数?

c++ - 后增量运算符行为

c++ - PangoLayout中忽略的新行

javascript - 一个javascript对象二义性问题

具有奇怪范围解析运算符的 C++ typedef

C++ 跨文件共享变量的良好做法?

c++ - 为什么 tuple_size 是特征而不是成员