私はコースでVisual Studio 2013を使用してC++を学習していますが、main.cpp
にインクルードガードに関する問題があります。私はclass
または#pragma once
のいずれかを使用することはできません。main.cppで複数のヘッダを使用する場合のインクルードガードの方法は?
私だけCoordinates
とLine
を使用して、私は(Line
とCoordinates
の両方からコードを取得)main.cpp
#include Line.h
で使用している場合、これは[OK]を動作しますが、私は(両方とも#include "Line.h"
を持っている)Rectangle
とTriangle
を追加するとき、それはスロー「既に定義されている」error LNK2005
何回か。
何かが欠けていますか?
これは私のコードです:
Coordinates.h
は#ifndef Coordinates
#define Coordinates
//Code declaration. Other headers have similar declaration
struct CoordinatesType { double x, y; } coordinates;
void setCoordinates(double x, double y);
CoordinatesType getCoordinates();
#endif
Coordinates.cpp
#include "Coordinates.h"
//Code implementation
Line.h
#ifndef Line
#define Line
#include "Coordinates.h"
//Code declaration
#endif
Line.cpp
#include "Line.h"
#include <math.h>
//Code implementation
Rectangle.h
#ifndef Rectangle
#define Rectangle
#include "Line.h"
//Code declaration
#endif
Rectangle.cpp
#include "Rectangle.h"
//Code implementation
Triangle.h
#ifndef Triangle
#define Triangle
#include "Line.h"
//Code declaration
#endif
main.cppに
#include "Triangle.h"
#include "Rectangle.h"
int main(){
//Do stuff here.
}
実装コードと宣言コードを追加する必要がある場合はお知らせくださいが、インクルードガードと関係があります。
編集:コーディネートヘッダーにコードを追加して、自分が何をやっているのかを知ることができ、投稿で多くのスペースを消費することを避けることができます。覚えておいて、私はclass Coordinates{}
を私の教授の制限のために使用することはできません。
http://stackoverflow.com/help/mcve – melpomene
どのようにクラスの名前が付けられていますか?インクルードガードとしての 'Line'? – Jarod42
あなたの教授は前方宣言の使用を禁止していますか?彼/彼女はあなたに何を教えているのですか? –