我有一个包含在其成员中的另一个类的对象的类。
头文件看起来像这样:
class LinxArduinoEthernetListener : public LinxListener
{
EthernetServer ArduinoTcpServer(uint16_t);
EthernetServer本身是一个类(在Arduino EthernetServer.h库中定义)。
因为在编译时我不知道端口,但是服务器对象应该是侦听器类的成员,所以我允许在类构造函数中初始化服务器对象,然后尝试使用稍后重新分配该对象。以下代码(在相应的.cpp文件中):
ArduinoTcpServer = EthernetServer(port);
其中“端口”是uint16_t。据我所知,这是将新构造的实例重新分配给对象变量的正确方法。
但是,编译器给我以下错误:
LinxArduinoEthernetListener.cpp:122:错误:成员的无效使用 函数(您是否忘记了'()'?)
ArduinoTcpServer = EthernetServer((uint16_t)port);
我认为这可能与我在随后的函数调用中遇到的错误有关:
LinxArduinoEthernetListener.cpp:123:错误: '(((LinxArduinoEthernetListener *)this)-> LinxArduinoEthernetListener :: ArduinoTcpServer' 没有类类型
ArduinoTcpServer.begin();
但是我要说的是,它确实具有一个类类型,即头文件中指定的EthernetServer类。
我在这里做什么错了?
答案 0 :(得分:2)
EthernetServer ArduinoTcpServer(uint16_t);
声明一个名为ArduinoTcpServer
的成员函数。要声明成员变量,请省略参数类型和括号。还添加一个构造函数以初始化成员变量,例如:
class LinxArduinoEthernetListener : public LinxListener
{
public:
EthernetServer ArduinoTcpServer;
LinxArduinoEthernetListener(uint16_t port)
: ArduinoTcpServer(port)
{
}
};
答案 1 :(得分:0)
所以,问题在于服务器对象的声明。
最初,我声明如下:
func preloadData() {
let fetch: NSFetchRequest<Season> = Season.fetchRequest()
fetch.predicate = NSPredicate(format: "searchKey != nil")
let count = try! managedContext.count(for: fetch)
if count > 0 { return }
let path = Bundle.main.path(forResource: "Preload", ofType: "plist")
let dataArray = NSArray(contentsOfFile: path!)!
for dict in dataArray {
let entity = NSEntityDescription.entity(forEntityName: "Season", in: managedContext)!
let season = Season(entity: entity, insertInto: managedContext)
let seasonDict = dict as! [String: Any]
season.name = seasonDict["name"] as? String
season.tasks = seasonDict["tasks"] as? NSSet
}
try! managedContext.save()
}
但是我会收到关于22的“数值常数之前的预期标识符”的错误。所以我用Google搜索它,发现有人建议(在某些情况下我不记得了)仅指定类型足以调用与该原型匹配的构造函数。这样做允许编译器继续运行,因此我认为这是有效的。但是,似乎没有。
真正的问题是,Arduino IDE使用的编译器需要大括号初始化,例如
> for dict in dataArray
- Task > title
- Task > detail
Here is one <dict> from the <array>
<array>
<dict>
<key>season</key>
<string>Early Winter</string>
<key>tasks</key>
<array>
<dict>
<key>title</key>
<string>Check water softener and add salt if needed</string>
<key>details</key>
<string></string>
</dict>
<dict>
<key>title</key>
<string>Inspect, and change HVAC filters if they are dirty</string>
<key>details</key>
<string>If you have no pets or allergies and are a small family, you can change filters only every 2-3 months.</string>
<key>dueDate</key>
<string></string>
</dict>
<dict>
<key>title</key>
<string>Clean kitchen sink disposals</string>
<key>details</key>
<string>Freeze vinegar in an ice cube tray and run the ice cubes through the disposal to freshen it and sharpen the blades.</string>
<key>dueDate</key>
<string></string>
</dict>
<dict>
<key>title</key>
<string>Clean range hood filters</string>
<key>details</key>
<string></string>
<key>dueDate</key>
<string></string>
</dict>
<dict>
<key>title</key>
<string>Inspect fire extinguishers</string>
<key>details</key>
<string>Inspect fire extinguishers to ensure that you can get easy access to them, the gauges show adequate pressure, and they have no visible signs of wear.</string>
<key>dueDate</key>
<string></string>
</dict>
</array>
</dict>
...
</array>
这似乎可行。