I have two mutually dependent header files: BarP.h and addNewProductForm.h, built using Microsoft CLR components, which look something like this (they are way too long to include intact):
BarP.h:
#pragma once
#include "addNewProductForm.h"
#include "editBarOptionsForm.h"
#include "editDecayParamsForm.h"
#include "editExistingItemForm.h"
#include <math.h>
#include <string>
#include <iostream>
#include <fstream>
#include <msclr\marshal.h>
namespace BarPricer3 {
using namespace std;
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace msclr::interop;
ref struct productDat{...};
productDat^ createProduct(String^ name,double firstPrice,double lastPrice,double lastDemand){...};
public ref class BarP{
...
private: System::Void createNewProductForm(...){
BarPricer3::addNewProductForm^ newProductForm = gcnew addNewProductForm;
newProductForm->ShowDialog(this);
}
}
addNewProductForm.h
#pragma once
#include "BarP.h"
#include <fstream>
namespace BarPricer3 {
using namespace std;
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public ref class addNewProductForm{
...
private: System::Void createNewProduct(...){
productDat^ theNewP = createProduct(this->name->Text,Convert::ToDouble(this->firstPrice->Text),Convert::ToDouble(this->lastPrice->Text),Convert::ToDouble(this->lastDemand->Text));
...
}
}
When I try to compile, I get the following errors:
Error 8 error C2039: ‘addNewProductForm’ : is not a member of ‘BarPricer3’ d:…\BarP.h 690 (line 32 in my code)
Error 15 error C2065: ‘productDat’ : undeclared identifier d:…\addNewProductForm.h 181 (line 19 in my code)
Can I get any advice about what’s happening here?
You have a circular inclusion in the header files, and that, combined with the
#pragma oncedirective, is what yields the error. You need to remove one of the includes (or both) and replace them with a forward declaration (might want to google this).You’ll probably need to separate the implementations to a different file, because you use the types inside the headers.
Your problem is that
addNewProductForm.husesproductDatwhich is defined inBarP.h, and thatBarP.husesaddNewProductFormwhich is defined inaddNewProductForm.h. (These are the classes that will need to be forward-declared).