程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了由于在 C++ 中不正确地实现类而导致的错误大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决由于在 C++ 中不正确地实现类而导致的错误?

开发过程中遇到由于在 C++ 中不正确地实现类而导致的错误的问题如何解决?下面主要结合日常开发的经验,给出你关于由于在 C++ 中不正确地实现类而导致的错误的解决方法建议,希望对你解决由于在 C++ 中不正确地实现类而导致的错误有所启发或帮助;

我写了下面的程序,根据我的理解,最终得到了下面的代码。那里
似乎仍然存在大量错误,例如:

  1. IDentifIEr "lAmbertian" is undefinedC/C++ 在文本编辑器中 - 用于 auto material_ground = make_shared<lAmbertian>(color(0.8,0.8,0.0)); 中的朗伯式

2) pointer to incomplete class type "material" is not allowed - 用于记录 if (rec.mat_ptr->scatter(r,rec,attenuation,scattered)) 在编辑器中

我在编译时也遇到了多个错误,例如:

  1. vec3.h: In member function 'vec3 vec3::reflect(const vec3&,const vec3&)': vec3.h:61:33: error: 'dot' was not declared in this scope return v - 2*dot(v,n)*n;

  2. invalID use of incomplete type 'class hittable_List' class hittable_List : public hittable_List

  3. sphere.h:7:7: note: candIDate expects 1 argument,2 provIDed sphere.h:7:7: note: candIDate: sphere::sphere(sphere&&)

而且我认为我很可能不正确地实现了类,所以有没有任何资源可以帮助我在不久的将来避免此类错误?

代码:

chap_8.cpp:

    #include "camera.h"
#include "rtweekend.h"
#include "vec3.h"
#include "color.h"
#include "hittableList.h"
#include "sphere.h"
#include "ray.h"
#include <iostream>
#include <fstream>
#include "material.h"
#include "hittable.h"

color ray_color(const ray& r,const hittable_List& world,int depth) {
    hit_record rec;

    // If we've exceeded the ray bounce limit,no more light is gathered.
    if (depth <= 0)
        return color(0,0);

    if (world.hit(r,0.001,infinity,reC)) {
        ray scattered;
        color attenuation;
        if (rec.mat_ptr->scatter(r,scattered))
            return attenuation * ray_color(scattered,world,depth-1);
        return color(0,0);
    }

    vec3 unit_direction = unit_vector(r.direction());
    auto t = 0.5*(unit_direction.y() + 1.0);
    return (1.0-t)*color(1.0,1.0,1.0) + t*color(0.5,0.7,1.0);
}



int main() {

    // Image

    const auto aspect_ratio = 16.0 / 9.0;
    const int image_wIDth = 400;
    const int image_height = static_cast<int>(image_wIDth / aspect_ratio);
    const int samples_per_pixel = 100;
    const int max_depth = 50;

    // World

    hittable_List world;
    auto material_ground = make_shared<lAmbertian>(color(0.8,0.0));
    auto material_center = make_shared<lAmbertian>(color(0.7,0.3,0.3));
    auto material_left   = make_shared<Metal>(color(0.8,0.8),0.3);
    auto material_right  = make_shared<Metal>(color(0.8,0.6,0.2),1.0);

    world.add(make_shared<sphere>(point3( 0.0,-100.5,-1.0),100.0,material_ground));
    world.add(make_shared<sphere>(point3( 0.0,0.0,0.5,material_center));
    world.add(make_shared<sphere>(point3(-1.0,material_left));
    world.add(make_shared<sphere>(point3( 1.0,material_right));

    // Camera

    camera cam;

    // Render

    std::cout << "P3\n" << image_wIDth << " " << image_height << "\n255\n";

    for (int j = image_height-1; j >= 0; --j) {
        std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
        for (int i = 0; i < image_wIDth; ++i) {
            color pixel_color(0,0);
            for (int s = 0; s < samples_per_pixel; ++s) {
                auto u = (i + random_double()) / (image_wIDth-1);
                auto v = (j + random_double()) / (image_height-1);
                ray r = cam.get_ray(u,v);
                pixel_color += ray_color(r,max_depth);
            }
            write_color(std::cout,pixel_color,samples_per_pixel);
        }
    }

    std::cerr << "\nDone.\n";
}

hittableList.h

    #ifndef HITtable_List_H
#define HITtable_List_H

#include "hittable.h"

#include <memory>
#include <vector>

using std::shared_ptr;
using std::make_shared;

class hittable_List : public hittable_List {
    public:
        hittable_List() {}
        hittable_List(shared_ptr<hittable_List> object) { add(object); }

        voID clear() { objects.clear(); }
        voID add(shared_ptr<hittable_List> object) { objects.push_BACk(object); }

        virtual bool hit(
            const ray& r,double t_min,double t_max,hit_record& reC) const overrIDe;

    public:
        std::vector<shared_ptr<hittable_List>> objects;
};

bool hittable_List::hit(const ray& r,hit_record& reC) const {
    hit_record temp_rec;
    bool hit_anything = false;
    auto closest_so_far = t_max;

    for (const auto& object : objects) {
        if (object->hit(r,t_min,closest_so_far,temp_reC)) {
            hit_anything = true;
            closest_so_far = temp_rec.t;
            rec = temp_rec;
        }
    }

    return hit_anything;
}

#endif

sphere.h

  #ifndef SPHERE_H
#define SPHERE_H

#include "hittable.h"
#include "vec3.h"

class sphere : public hittable_List {
public:
    sphere() {}
    sphere(point3 cen,double r,shared_ptr<material> m)
        : center(cen),radius(r),mat_ptr(m) {};
    virtual bool hit(
        const ray& r,hit_record& reC) const overrIDe;

    public:
    point3 center;
    double radius;
    shared_ptr<material> mat_ptr;
    };

bool sphere::hit(const ray& r,hit_record& reC) const {
vec3 oc = r.origin() - center;
auto a = r.direction().length_squared();
auto half_b = dot(oc,r.direction());
auto c = oc.length_squared() - radius*radius;

auto discriminant = half_b*half_b - a*c;
if (discriminant < 0) return false;
auto sqrtd = sqrt(discriminant);

// Find the nearest root that lIEs in the acceptable range.
auto root = (-half_b - sqrtd) / a;
if (root < t_min || t_max < root) {
    root = (-half_b + sqrtd) / a;
    if (root < t_min || t_max < root)
        return false;
}

rec.t = root;
rec.p = r.at(rec.t);
vec3 outWARD_normal = (rec.p - center) / radius;
rec.set_face_normal(r,outWARD_normal);
rec.mat_ptr = mat_ptr;
return true;
}

#endif

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的由于在 C++ 中不正确地实现类而导致的错误全部内容,希望文章能够帮你解决由于在 C++ 中不正确地实现类而导致的错误所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。