第一次使用堆栈溢出。我正在尝试实践面向对象的抽象和接口。我一直无法编译程序,我的程序如下。
主程序
#include "Spells.h"
#include <iostream>
int main()
{
spell MagicalSpell;
std::cout << "Hello World!\n";
}
Run Code Online (Sandbox Code Playgroud)
Spells.h
#pragma once
class spell {
private:
int EnergyCost;
public:
spell();
~spell();
void SpellEffect();
};
Run Code Online (Sandbox Code Playgroud)
拼写.cpp
#include "Spells.h"
#include <iostream>
spell::spell() {
EnergyCost = 0;
}
spell::~spell() {
}
void spell::SpellEffect(){
std::cout << "woosh" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
每次我尝试编译 main.cpp 时,我都会得到:
g++ main.cpp -lcrypt -lcs50 -lm -o main
/tmp/ccnXk1TN.o: In function `main':
main.cpp:(.text+0x20): undefined reference to `spell::spell()'
main.cpp:(.text+0x3f): undefined reference to `spell::~spell()'
main.cpp:(.text+0x64): undefined reference …Run Code Online (Sandbox Code Playgroud)