我正在定义一个工厂来创建接口实现的对象。工厂包括接口标头以及实现的标头。两个类都在同一名称空间中。然而,在编译工厂时,编译器会给出错误C2065(IntelliSense会解决所有问题,如果这对任何事情都有效)
Public Class Database
Private m_cnADOConnection As New ADODB.Connection
Private m_rstAddress As New ADODB.Recordset
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
m_cnADOConnection.Open("Provider = Microsoft.Jet.OLEDB.4.0;" & "Data Source= AddressBook.mdb") ' connects and sets path for database
m_rstAddress.Open("tblContacts", m_cnADOConnection, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
ShowCurrentRecord() 'calls a method to fill the record set
Me.WindowState = FormWindowState.Normal ' opens form in different states
End Sub
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
ShowCurrentRecord()
Dim Search As String
Dim SearchLoop As Boolean = False ' sets the loop control
Search = InputBox("Enter Surname: ", "Search")
m_rstAddress.MoveFirst() ' set db to start of file
If IsNumeric(Search) Then
MessageBox.Show("Invalid Data: Surname must not be numeric", "Invalid Data")
Exit Sub ' check that valid entry has been made
End If
Do While SearchLoop = False
If m_rstAddress.EOF() Then
MessageBox.Show("No Matching Records Found !", "Nil Found", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
SearchLoop = True
ElseIf Search = CStr(m_rstAddress.Fields("Surname").Value) Then
ShowCurrentRecord()
SearchLoop = True
Else
m_rstAddress.MoveNext()
End If
Loop
End Sub
Private Sub ShowCurrentRecord()
If m_rstAddress.BOF Or m_rstAddress.EOF Then
txtFirstName.Text = ""
txtLastName.Text = ""
txtAddress1.Text = ""
txtAddress2.Text = ""
txtAddress3.Text = ""
txtPostCode.Text = ""
txtPhone.Text = ""
txtEmail.Text = ""
txtNotes.Text = ""
Exit Sub
End If
txtFirstName.Text = CStr(m_rstAddress.Fields("FirstName").Value) ' the following assigns the text box the value from the DB
txtLastName.Text = CStr(m_rstAddress.Fields("Surname").Value)
txtAddress1.Text = CStr(m_rstAddress.Fields("Address1").Value)
txtAddress2.Text = CStr(m_rstAddress.Fields("Address2").Value)
txtAddress3.Text = CStr(m_rstAddress.Fields("Address3").Value)
txtPostCode.Text = CStr(m_rstAddress.Fields("Postcode").Value)
txtPhone.Text = CStr(m_rstAddress.Fields("Phone").Value)
txtEmail.Text = CStr(m_rstAddress.Fields("Email").Value)
txtNotes.Text = CStr(m_rstAddress.Fields("Notes").Value)
End Sub
Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click
txtFirstName.Text = ""
txtLastName.Text = ""
txtAddress1.Text = ""
txtAddress2.Text = ""
txtAddress3.Text = ""
txtPostCode.Text = ""
txtPhone.Text = ""
txtEmail.Text = ""
txtNotes.Text = ""
MessageBox.Show("Please enter the details in the boxes then click Save Record", "Instructions")
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
m_rstAddress.AddNew()
m_rstAddress.Fields("FirstName").Value = txtFirstName.Text
m_rstAddress.Fields("Surname").Value = txtLastName.Text
m_rstAddress.Fields("Address1").Value = txtAddress1.Text
m_rstAddress.Fields("Address2").Value = txtAddress2.Text
m_rstAddress.Fields("Address3").Value = txtAddress3.Text
m_rstAddress.Fields("Phone").Value(-lblPhone.Text)
End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
m_rstAddress.AddNew()
m_rstAddress.Fields("FirstName").Value = txtFirstName.Text
m_rstAddress.Fields("Surname").Value = txtLastName.Text
m_rstAddress.Fields("Address1").Value = txtAddress1.Text
m_rstAddress.Fields("Address2").Value = txtAddress2.Text
m_rstAddress.Fields("Address3").Value = txtAddress3.Text
m_rstAddress.Fields("Postcode").Value = txtPostCode.Text
m_rstAddress.Fields("Phone").Value = txtPhone.Text
m_rstAddress.Fields("Email").Value = txtEmail.Text
m_rstAddress.Fields("Notes").Value = txtNotes.Text
m_rstAddress.Update()
ShowCurrentRecord()
If m_rstAddress.EOF Then
m_rstAddress.MoveFirst()
ShowCurrentRecord()
Else : m_rstAddress.MoveFirst()
ShowCurrentRecord()
End If
m_rstAddress.Update()
ShowCurrentRecord()
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Me.Close()
End Sub
Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
If m_rstAddress.BOF Then
MessageBox.Show("You have reached the begninning of the file.", "BOF")
m_rstAddress.MoveFirst()
ShowCurrentRecord()
Else : m_rstAddress.MoveFirst()
ShowCurrentRecord()
Elsee: m_rstAddress.MovePrevious()
ShowCurrentRecord()
End If
End Sub
Private Sub btnPrevious_Click(sender As System.Object, e As System.EventArgs) Handles btnPrevious.Click
If m_rstAddress.BOF Then
MessageBox.Show("You have reached the end of this file.")
m_rstAddress.MoveFirst()
ShowCurrentRecord()
Else : m_rstAddress.MovePrevious()
ShowCurrentRecord()
End If
End Sub
Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
If m_rstAddress.BOF Then
m_rstAddress.MoveLast()
ShowCurrentRecord()
Else : m_rstAddress.MoveLast()
ShowCurrentRecord()
End If
End Sub
End Class
界面定义了很多功能,在我看来,这些功能对这个问题没有任何作用。所有功能都是纯虚拟的,并在实现中实现。
#pragma once
#include <memory>
#include "IDBConnector.hpp"
class IDBConnectorFactory
{
public:
virtual ~IDBConnectorFactory() { }
virtual std::shared_ptr<IDBConnector> createDBConnector() = 0; ///< Create a new object of an implementation of the IDBConnector interface
};
编译器输出为:
#pragma once
#include <string>
#include "ConfigFormats.hpp"
class IDBConnector
{
public:
virtual ~IDBConnector(){};
virtual bool connect() = 0;
virtual std::string getSchema() = 0;
...
};
英文应该如下:
error C2065: "IDBConnector": nichtdeklarierter Bezeichner
error C2923: "std::shared_ptr": "IDBConnector" ist kein gültiges template-Typargument für den _Ty-Parameter.
可能是我弄乱了配置吗?我试图禁用预编译头的使用,但它没有改变编译错误。
我使用doxygen创建了一个依赖图,以查看循环依赖所在的位置:
我通过将EDLBackend.hpp中的东西放到.cpp文件中并在EDLBackend.hpp中声明IDBConnectorFactory和IDBConnector来解决循环问题。 然后我做了另一个循环依赖和tada的整个事情,它的工作原理。谢谢你们!
答案 0 :(得分:2)
我敢打赌IDBConnector.hpp
开始了:
#pragma once
#include <memory>
#include "IDBConnectorFactory.hpp"
扩展它,但包括前几行,我们有:
#pragma once
#include <memory>
// #pragma once
// #include <memory> - skipped
// #incude "IDBConnector.hpp" - skipped because of #pragma once
class IDBConnectorFactory
{ ....
因此定义IDBConnectorFactory
时未定义IDBConnector
。
修复是其中一个标题必须停止包括另一个标题。您可以 可能会从IDBConnector.hpp中删除#include“IDBConnectorFactory.hpp”,只需用一行代替:
class IDConnectorFactory;
(这告诉编译器有类,所以它可以创建引用和指针,但没有细节。)
答案 1 :(得分:0)
您可能在头文件中使用多重保护。 试试这个:
ENABLE_PREPROCESSING = NO