编译用于在C ++中写入文件的错误

时间:2014-11-11 15:11:46

标签: c++ compiler-errors

有人可以解释一下我做错了什么。根据编译器的输出,每行必须至少包含一个错误。但是,由于我发现了具有常量char数组cplusplus.com tutorial的写入示例,因此C2297错误似乎没有多大意义?

#include <iostream>
#include <fstream>
#include "stdafx.h"
#include "accelerators/tree.h"
#include "paramset.h"
#include "shapes/trianglemesh.h"

void Tree::PrintTree() const {
    std::ofstream myfile;
    myfile.open("RTSAH.txt");

    for (uint32_t i = 0; i < nextFreeNode; ++i) {
        const KdAccelNode* node = &nodes[i];
        if (node->IsLeaf()) {
            myfile << i;
            myfile << " L ";
            myfile << node->nPrimitives() << std::endl;
        }
        else {
            myfile << i;
            myfile << " I ";
            myfile << "SP= ";
            myfile << node->SplitPos();
            myfile << " SA= ";
            myfile << node->SplitAxis();
            myfile << " BC= ";
            myfile << (i + 1);
            myfile << " AC= ";
            myfile << node->AboveChild() << std::endl;
        }
    }
    myfile.close();
}

enter image description here

2 个答案:

答案 0 :(得分:1)

由于第一个错误报告无法确定myfile是什么,因此使用myfile的任何后续代码几乎肯定会产生错误,即使它是正确的。

答案 1 :(得分:0)

  

stdafx.h标头必须是要包含在其中的第一个标头   .c / .cpp文件。这是必须的!否则,您可以保证   出现编译错误。

来源:StdAfx.h for Novices

因此,以下内容包括

#include <iostream>
#include <fstream>
#include "stdafx.h"

应重新排序为

#include "stdafx.h"
#include <iostream>
#include <fstream>

确保现在将包含<iostream><fstream>,因此将定义std::basic_ofstream。这解决了与局部变量myfile有关的第一个错误,因此也解决了与该局部变量myfile相关的所有其他错误(在other answer中猜测/预期)