【问题标题】:'ios' : is not a class or namespace name'ios' : 不是类或命名空间名称
【发布时间】:2012-09-22 22:42:00
【问题描述】:

我正在尝试使用上述代码将矩阵写入文件。但我收到以下错误: 'ios' :不是类或命名空间名称。我的代码:

std::ofstream myfile;
myfile.open ("C:/Users/zenitis/Desktop/bots/Nova/data/ownStatus.txt", ios::out | ios::app);               

for (int i = 0; i< 21; i++){
    myfile << featureMatrix[i] << "          ";
}
myfile << "\n";
myfile.close();

对这个问题有任何想法吗??

【问题讨论】:

  • std::ios::out

标签: c++ file iostream


【解决方案1】:

iosstd 的成员。也就是说,您想使用以下方法之一来引用它:

using namespace std; // bad
using std::ios;      // slightly better

int main() {
    std::ofstream myFile("name", std::ios::app); // best
}

顺便说一句,你可以直接在构造函数中open()std::ofstream。此外,对于std::ofstream,标志std::ios_base::out(打开标志实际上是在std::ios 的基类std::ios_base 中定义的)会自动添加。

【讨论】:

  • using namespace 在函数本身中是可以的,但在标题和整个代码中都很好,没有。
  • 函数定义之外的 using 指令或 using 声明在标头中绝对没有问题。在源文件中是否可以接受(包括所有标题之后)是一个品味问题。就个人而言,我不喜欢它们中的任何一个,并且我倾向于使用完整的限定词,除非有问题的名称打算通过依赖于参数的查找来潜在地找到。
  • 你不能也用I.在函数本身int main() { using std::ios::out;
【解决方案2】:

其实是std::ios::out

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 2012-05-28
    • 1970-01-01
    相关资源
    最近更新 更多