我正试图重新使用C ++。因此,我决定按照一个教程系列进行学习。但是,我无法在CLion中编译以下代码。我想我在undefined reference to Class::Function() C++找到了一个修复程序,但并没有真正了解它的工作方式或如何避免它。我的代码如下:
Sally.cpp-C:\ Users \ bhave \ OneDrive \ Documents \ CLion Projects \ C ++ Tuts \ Sally.cpp
//
// Created by bhave on 4/18/2019.
//
#include "Sally.h"
#include <iostream>
using namespace std;
Sally::Sally(int a, int b) {
regVar = a;
constVar = b;
}
Sally.h-C:\ Users \ bhave \ OneDrive \ Documents \ CLion Projects \ C ++ Tuts \ Sally.h
//
// Created by bhave on 4/18/2019.
//
#ifndef C___TUTS_SALLY_H
#define C___TUTS_SALLY_H
class Sally {
public:
Sally(int a, int b);
private:
int regVar;
const int constVar;
};
#endif //C___TUTS_SALLY_H
AllRun.cpp-C:\ Users \ bhave \ OneDrive \ Documents \ CLion Projects \ C ++ Tuts \ All run.cpp
//
// Created by bhave on 4/6/2019.
//
#include <iostream>
#include "Important Tutorial Files/RandNumGen.h"
#include "Important Tutorial Files/Default Value Functions.cpp"
#include "Important Tutorial Files/Tutorial 29.cpp"
#include "Sally.h"
using namespace std;
// Default Value Function
void fn0() {
cout << volume() << endl;
cout << volume(10) << endl;
cout << volume(10, 4) << endl;
cout << volume(10, 4, 8) << endl;
}
// Random Number Generator Example
void fn1() {
cout << RandNumGen::genRand() << endl;
cout << RandNumGen::genRangeRand(1, 6) << endl;
}
int main() {
Sally sally(23, 42);
fn0();
fn1();
return 0;
}
CMakeLists.txt-C:\ Users \ bhave \ OneDrive \ Documents \ CLion Projects \ C ++ Tuts \ CMakeLists.txt
cmake_minimum_required(VERSION 3.13)
project(C___Tuts)
set(CMAKE_CXX_STANDARD 14)
add_executable(C___Tuts "All run.cpp")
编辑:我认为这是一个链接器错误,但是我以前从未遇到过,所以如果有人可以解释在创建文件时如何在CLion中避免它,那将是很好的事情。