Issue of 'Uncaught TypeError: Callback must be a function' using C++ in Atom editor
Issue of 'Uncaught TypeError: Callback must be a function' using C++ in Atom editor
我在 Mac 上使用 Atom 编译和 运行 C++ 代码,但是我在简单程序上遇到此错误。我是 C++ 的新手。请帮忙!
注意:我能够 运行 在 Xcode.
中做到这一点
main.cpp 文件:
#include "Cube.h"
#include <iostream>
int main() {
Cube c;
c.setLength(3.48);
double volume = c.getVolume();
std::cout << "Volume: " << volume << std::endl;
return 0;
}
Cube.h 文件:
#pragma once
class Cube {
public:
double getVolume();
double getSurfaceArea();
void setLength(double length);
private:
double length_;
};
Cube.cpp 文件:
#include "Cube.h"
double Cube::getVolume() {
return length_ * length_ * length_;
}
double Cube::getSurfaceArea() {
return 6 * length_ * length_;
}
void Cube::setLength(double length) {
length_ = length;
}
您需要一个真正的构建系统。 Atom-gpp 仅适用于单个 .cpp 文件。
我建议您花些时间研究 CMake,这是一个(主要)用于 C 和 C++ 的现代构建系统。
根据您的项目,将以下内容放在您的 .cpp 文件旁边的 CMakeLists.txt
中:
cmake_minimum_required(VERSION 3.10)
project(course1)
add_executable(course1 main.cpp Cube.cpp)
这三行足以告诉 CMake 您要生成一个程序 course1
由两个 .cpp 文件组成。 CMake 足够聪明,可以只编译您在两次运行之间实际更改的文件。
然后您可以安装像 AtomBuild 这样的插件,它将为您检测 CMake 文件并添加必要的构建集成和 运行。
或从终端调用 CMake:
$ cmake . # generates the build files
$ cmake --build . # actually builds
注意:您可能需要先安装 CMake,在 macOS 上,规范的安装方法是使用 Homebrew 包管理器:
$ brew install cmake
我在 Mac 上使用 Atom 编译和 运行 C++ 代码,但是我在简单程序上遇到此错误。我是 C++ 的新手。请帮忙!
main.cpp 文件:
#include "Cube.h"
#include <iostream>
int main() {
Cube c;
c.setLength(3.48);
double volume = c.getVolume();
std::cout << "Volume: " << volume << std::endl;
return 0;
}
Cube.h 文件:
#pragma once
class Cube {
public:
double getVolume();
double getSurfaceArea();
void setLength(double length);
private:
double length_;
};
Cube.cpp 文件:
#include "Cube.h"
double Cube::getVolume() {
return length_ * length_ * length_;
}
double Cube::getSurfaceArea() {
return 6 * length_ * length_;
}
void Cube::setLength(double length) {
length_ = length;
}
您需要一个真正的构建系统。 Atom-gpp 仅适用于单个 .cpp 文件。
我建议您花些时间研究 CMake,这是一个(主要)用于 C 和 C++ 的现代构建系统。
根据您的项目,将以下内容放在您的 .cpp 文件旁边的 CMakeLists.txt
中:
cmake_minimum_required(VERSION 3.10)
project(course1)
add_executable(course1 main.cpp Cube.cpp)
这三行足以告诉 CMake 您要生成一个程序 course1
由两个 .cpp 文件组成。 CMake 足够聪明,可以只编译您在两次运行之间实际更改的文件。
然后您可以安装像 AtomBuild 这样的插件,它将为您检测 CMake 文件并添加必要的构建集成和 运行。
或从终端调用 CMake:
$ cmake . # generates the build files
$ cmake --build . # actually builds
注意:您可能需要先安装 CMake,在 macOS 上,规范的安装方法是使用 Homebrew 包管理器:
$ brew install cmake