私は3次元空間の数学的ベクトルを含むプロジェクトに取り組んでいます(vector
コレクションタイプと混同しないでください)。私はVector.cpp
に定義されたclass Vector
を持っており、Vector.h
と宣言しています。次のように私のディレクトリ構造は次のとおりです。両方のファイルが同じディレクトリにあるのにリンカエラーが発生するのはなぜですか?
私はプロジェクトをビルドしようとすると、私はLNK2019
未解決の外部シンボルのエラーが発生します。私の知る限りでは、私の3つのファイルは全てビルドパス上にあります。
Vector.cpp
で:
class Vector
{
private:
double xComponent;
double yComponent;
double zComponent;
public:
Vector(double x, double y, double z) : xComponent(x), yComponent(y), zComponent(z) {}
double dotProduct(const Vector& other) const
{
return xComponent * other.xComponent + yComponent * other.yComponent + zComponent * other.zComponent;
}
}
Vector.h
において:
#ifndef VECTOR_H
#define VECTOR_H
class Vector
{
public:
Vector(double x, double y, double z);
double dotProduct(const Vector& other) const;
}
#endif
Vectors.cpp
において:
#include "Vector.h"
#include <iostream>
using std::cout;
using std::endl;
int main()
{
Vector foo = Vector(3, 4, -7);
Vector bar = Vector(1.2, -3.6, 11);
cout << foo.dotProduct(bar) << endl;
return 0;
}
foo.dotProduct(bar)
リンカーエラーが発生した唯一の場所である(エラーがで発生していませんコンストラクタ)。私はVector
の他の非コンストラクタメソッドのいくつかを試しており、リンカエラーも発生しました。なぜコンストラクタは動作しますが、他のコンストラクタは動作しませんか?
これは、プロジェクトをビルドしようとするから出力された:
1>------ Build started: Project: Vectors, Configuration: Debug Win32 ------
1>Vectors.obj : error LNK2019: unresolved external symbol "public: double __thiscall Vector::dotProduct(class Vector const &)const " ([email protected]@@[email protected]@Z) referenced in function _main
1>C:\Users\John\Documents\Visual Studio 2017\Projects\Vectors\Debug\Vectors.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "Vectors.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
ベクターは、両方ではなく1つのファイルで宣言する必要があります。 –
@NeilButterworth私はあなたが何を意味しているのかよく分かりません。ヘッダーファイルに 'Vectors.cpp'で使用できるように前方宣言する必要はありませんか? – train1855
nope - vector.cppにはメソッド本体が必要です。また、実際のエラーテキスト – pm100