left函数未定义_c – 对构造函数和析构函数的未定义引用

该博客主要讨论了C++编程中遇到的一个编译错误,即在链接阶段出现对构造函数和析构函数的未定义引用。问题出现在一个名为`Sphere`的类及其在`Scene`类中的使用。博主通过分析`Makefile`和源代码文件,指出可能的原因是类对象没有正确链接。解决方案可能涉及检查链接选项和确保所有相关源文件被正确编译和包含。

我只是看到这是由人们在构建时没有将他们的类对象链接在一起引起的,但我有,而且我不确定是什么问题.

来源:

//test.cpp

#include "Point.h"

#include "Sphere.h"

#include "Scene.h"

int main(){

Sphere s;

Scene sc;

sc.img_plane.upper_left = Point(-1, 1, -3);

sc.img_plane.upper_right = Point(1, 1, -3);

sc.img_plane.lower_left = Point(-1, -1, -3);

sc.img_plane.lower_right = Point(1, -1, -3);

sc.width = 100;

sc.height = 100;

return 0;

}

错误:

test.cpp:(.text+0x17): undefined reference to `Sphere::Sphere()'

test.cpp:(.text+0x26): undefined reference to `Scene::Scene()'

test.cpp:(.text+0x192): undefined reference to `Scene::~Scene()'

test.cpp:(.text+0x1a1): undefined reference to `Sphere::~Sphere()'

test.cpp:(.text+0x1bf): undefined reference to `Scene::~Scene()'

test.cpp:(.text+0x1d3): undefined reference to `Sphere::~Sphere()'

Makefile:

CC = g++

SRC = ./src

BUILD = ./build

main : main.o Point.o colort.o Sphere.o Scene.o Ray.o HitRecord.o

$(CC) $(BUILD)/main.o $(BUILD)/Point.o -o $(BUILD)/main

main.o : $(SRC)/test.cpp

$(CC) -c $< -o $(BUILD)/main.o

Point.o : $(SRC)/Point.cpp $(SRC)/Point.h

$(CC) -c $< -o $(BUILD)/Point.o

colort.o : $(SRC)/colort.cpp $(SRC)/colort.h

$(CC) -c $< -o $(BUILD)/colort.o

Sphere.o : $(SRC)/Sphere.cpp $(SRC)/Sphere.h $(SRC)/colort.cpp $(SRC)/Point.cpp

$(CC) -c $< -o $(BUILD)/Sphere.o

ImagePlane.o : $(SRC)/ImagePlane.cpp $(SRC)/ImagePlane.h

$(CC) -c $< -o $(BUILD)/ImagePlane.o

Scene.o : $(SRC)/Scene.cpp $(SRC)/Scene.h

$(CC) -c $< -o $(BUILD)/Scene.o

Ray.o : $(SRC)/Ray.cpp $(SRC)/Ray.h

$(CC) -c $< -o $(BUILD)/Ray.o

HitRecord.o : $(SRC)/HitRecord.cpp $(SRC)/HitRecord.h

$(CC) -c $< -o $(BUILD)/HitRecord.o

clean :

rm $(BUILD)/*

test : main

$(BUILD)/main

场景构造函数与Sphere相同:

Sphere.h:

#ifndef SPHERE_H_

#define SPHERE_H_

#include "colort.h"

#include "Point.h"

#include "Ray.h"

class Sphere {

public:

Sphere();

virtual ~Sphere();

Point center;

double radius;

color_t diffuse, specular;

double intersection(Ray r);

};

#endif /* SPHERE_H_ */

Sphere.cpp:

#include "Sphere.h"

#include

Sphere::Sphere() {

}

Sphere::~Sphere() {

}

...other class functions

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值