以下是代码外观的粗略示例,问题是如何让DerivedOne和DerivedTwo具有重载的<<运算符,但将这些对象存储在Base*的向量中.
至于我想要达到的目标; 我希望能够遍历对象向量并输出我在DerivedOne和DerivedTwo中告诉它的信息.
vector<Base*> objects;
class Base
{
private:
object Data
public:
object getData() { return Data; }
};
class DerivedOne : public Base
{
}
class DerivedTwo : public Base
{
}
Run Code Online (Sandbox Code Playgroud)
现在我知道有这个,但它不适用于我的目的.
friend ostream &operator<<(ostream &stream, Object object)
{
return stream << "Test" << endl;
}
Run Code Online (Sandbox Code Playgroud) 我已经调试了这一点,我知道这个问题,我只是不知道如何解决它.
现在我正在使用:
file.open(logFile.c_str(), std::ios::out | std::ios::app);
Run Code Online (Sandbox Code Playgroud)
只要我注释掉下面标记的行,logFile变量就可以正常工作:
time_t rawtime;
struct tm * timeinfo;
time (&rawtime);
timeinfo = localtime (&rawtime);
logFile = "bin/";
if(_DEBUG) { logFile += "Debug/"; }
else { logFile += "Release/"; }
logFile += fileName;
if(_DEBUG) { logFile += "Debug-"; }
else { logFile += "Release-"; }
logFile += asctime(timeinfo); // Works fine with this line commented.
logFile += ".log";
Run Code Online (Sandbox Code Playgroud)
那么如何才能让这条线路正常工作呢?
所以我有我的Vector类,点和交叉产品的这两个重载运算符,我假设你不能这样做,我应该有一个交叉函数.
inline T operator *(const Vector3<T> &v)
{
return value[0]*v[0]+value[1]*v[1]+value[2]*v[2];
}
inline Vector3<T> operator *(const Vector3<T> &v)
{
Vector3<T> result;
result[0] = value[1]*v[2] - value[2]*v[1];
result[1] = value[2]*v[0] - value[0]*v[2];
result[2] = value[0]*v[1] - value[1]*v[0];
return result;
}
Run Code Online (Sandbox Code Playgroud)
如果有机会,有一种方法可以做到这一点很棒,是不是很有可能?