在主文件中声明名称空间不会继承到头文件:“ using namespace std;”

时间:2019-03-16 03:28:19

标签: c++ visual-studio visual-studio-2017 namespaces

我的教授为我的班级提供了一个包含我主要功能的.cpp文件,以及三个不同班级的三个头文件。在主文件中,他利用“ using namespace std;”。声明项目的名称空间。

我知道“使用命名空间标准”;这是一种不好的做法,应不惜一切代价避免,但老师坚持在项目完成时要注意三个要点:

  1. 您不得修改任何提供的文件。 “不是一个字符”。 (减少了60%的标记)
  2. 它无法在Visual Studio 2017中构建。(标记减少了60%)
  3. 在正常运行期间崩溃。 (减少了60%的分数)

我的问题是,在主文件中声明的名称空间(通过使用“ using namespace std;”使用)似乎没有继承到头文件。

这是主文件:

// ass2.cpp
#define _CRT_SECURE_NO_WARNINGS
#define _CRTDBG_MAP_ALLOC   // need this to get the line identification
//_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF|_CRTDBG_LEAK_CHECK_DF); // in main, after local declarations
//NB must be in debug build
#include <crtdbg.h>
#include <iostream>
#include <string>
#include <vector>
#include <forward_list>
using namespace std; // THE NAMESPACE DECLARATION
#include "Frame.h"
#include "Animation.h"
#include "AnimationManager.h"

int main(void)
{
    char response;
    bool RUNNING = true;
    AnimationManager M("Manager1");
    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

    while (RUNNING)
    {
        cout << "MENU\n 1. Add an Animation\n 2. Delete an Animation\n 3. Edit an Animation\n 4. list the Animations\n 5. Quit" << endl;
        cin >> response;
        switch (response)
        {
        case '1':cin >> M; break;
        case '2':M.DeleteAnimation(); break;
        case '3':M.EditAnimation(); break;
        case '4':cout << M; break;
        case '5':RUNNING = false; break;
        default:cout << "Please enter a valid option" << endl;
        }
    }
    return 0;
}

这是头文件之一,其余文件的结构相同:

//Animation.h
#pragma once

class Animation
{
    string animationName;
    forward_list<Frame> frames;
public:
    Animation(string name) :animationName(name) {}
    ~Animation() {}
    void EditFrame();
    void DeleteFrame();
    friend istream& operator>>(istream&, Animation&);// Add a Frame as in cin >> A;
    friend ostream& operator<<(ostream&, Animation&);// output the Frames as in cout << A;
};

我创建了一个重新创建事件的测试文件,using namespace std;语句放置在#include "xxx.h"语句之前。

我创建的测试空间的主文件:

//: C12:IostreamOperatorOverloading.cpp
// Modified
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
#define _CRT_SECURE_NO_WARNINGS
#define _CRTDBG_MAP_ALLOC   // need this to get the line identification
#include <iostream>
#include <crtdbg.h>
using namespace std;
#include "IntArray.h"

    int main() {
        char q;
        IntArray I, J;
        _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
        cout << "Overloaded extraction" << endl;
        cin >> I;
        cout << "Overloaded insertion" << endl;
        cout << I;
        cout << "Chained extraction" << endl;
        cin >> I >> J;
        cout << "Chained Insertion" << endl;
        cout << I << J;
        cout << "***********overloaded operator[] - returns an l-value*************" << endl << endl;
        I[4] = -1;
        J[0] = 99;
        cout << I << J;
        cout << "*******a returned r-value assigned to a returned l-value************" << endl << endl;
        I[0] = I[4];
        J[0] = I[1] = J[2];     // chained indexing
        cout << I << J;
        cout << endl << "********or, using explicit function calls (never do it this way) ********" << endl;
        operator>>(operator>>(cin, I), J);  //  cin >> I >> J;
        operator<<(operator<<(cout, I), J); //  cout << I <<  J;
        cout << "**************************************************" << endl;
        I.operator[](4) = -1;   // I[4] = -1;
        J.operator[](0) = 99;   // J[0] = 99;
        operator<<(operator<<(cout, I), J); // cout << I <<  J;
        cout << "**************************************************" << endl;
        I.operator[](0) = I.operator[](4);  // a returned r-value assigned to a returned l-value
        J.operator[](0) = I.operator[](1) = J.operator[](2);    // chained indexing
        operator<<(operator<<(cout, I), J);
        cout << "**************************************************" << endl;

        cin >> q;
    } ///:~

我创建的测试空间的头文件:

#pragma once

class IntArray {

    enum { sz = 5 };
    int i[sz];

public:

    IntArray() { memset(i, 0, sz * sizeof(*i)); }

    // Overloaded indexing operator
    int& operator[](int x) {
        return i[x];
    }

    // non-member friend overloaded operators - global functions
    // a friend has access to the private members of the class
    friend ostream&  operator<<(ostream& os, IntArray& ia); // overloaded insertion operator global function
    friend istream&  operator>>(istream& is, IntArray& ia); // overloaded extraction operator global function
};
我创建的测试空间的

.cpp文件,其中包含IntArray类函数的定义:

#include <iostream>
#include "IntArray.h"
using namespace std;

std::ostream& operator<<(std::ostream& os, IntArray& ia) {
    for (int j = 0; j < ia.sz; j++) {
        //    os << ia.i[j];    // OK, this is the primitive way.
        os << ia[j];        // Better, now we have overloaded indexing.
        if (j != ia.sz - 1)
            os << ", ";
    }
    os << std::endl;
    return os;
}

std::istream& operator>>(std::istream& is, IntArray& ia) {
    for (int j = 0; j < ia.sz; j++)
        //   is >> ia.i[j]; // OK, this is the primitive way.
        is >> ia[j];        // Better, now we have overloaded indexing.
    return is;
}

最后,这是从测试文件重新出现的错误中出现的错误示例:

1>------ Build started: Project: PTR, Configuration: Debug Win32 ------
1>IntArray.cpp
1>c:\users\ali-g\desktop\cst8219\ptr\ptr\intarray.h(19): error C2143: syntax error: missing ';' before '&'
1>c:\users\ali-g\desktop\cst8219\ptr\ptr\intarray.h(19): error C2433: 'ostream': 'friend' not permitted on data declarations
1>c:\users\ali-g\desktop\cst8219\ptr\ptr\intarray.h(19): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ali-g\desktop\cst8219\ptr\ptr\intarray.h(19): error C2238: unexpected token(s) preceding ';'
1>c:\users\ali-g\desktop\cst8219\ptr\ptr\intarray.h(20): error C2143: syntax error: missing ';' before '&'
1>c:\users\ali-g\desktop\cst8219\ptr\ptr\intarray.h(20): error C2433: 'istream': 'friend' not permitted on data declarations
1>c:\users\ali-g\desktop\cst8219\ptr\ptr\intarray.h(20): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ali-g\desktop\cst8219\ptr\ptr\intarray.h(20): error C2238: unexpected token(s) preceding ';'
1>c:\users\ali-g\desktop\cst8219\ptr\ptr\intarray.cpp(6): error C2248: 'IntArray::sz': cannot access private enumerator declared in class 'IntArray'
1>c:\users\ali-g\desktop\cst8219\ptr\ptr\intarray.h(5): note: see declaration of 'IntArray::sz'
1>c:\users\ali-g\desktop\cst8219\ptr\ptr\intarray.h(3): note: see declaration of 'IntArray'
1>c:\users\ali-g\desktop\cst8219\ptr\ptr\intarray.cpp(9): error C2248: 'IntArray::sz': cannot access private enumerator declared in class 'IntArray'
1>c:\users\ali-g\desktop\cst8219\ptr\ptr\intarray.h(5): note: see declaration of 'IntArray::sz'
1>c:\users\ali-g\desktop\cst8219\ptr\ptr\intarray.h(3): note: see declaration of 'IntArray'
1>c:\users\ali-g\desktop\cst8219\ptr\ptr\intarray.cpp(17): error C2248: 'IntArray::sz': cannot access private enumerator declared in class 'IntArray'
1>c:\users\ali-g\desktop\cst8219\ptr\ptr\intarray.h(5): note: see declaration of 'IntArray::sz'
1>c:\users\ali-g\desktop\cst8219\ptr\ptr\intarray.h(3): note: see declaration of 'IntArray'
1>Done building project "PTR.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我发现通过添加“ using namespace std;”头文件顶部的语句(这是非常糟糕的做法)解决了这个问题,但是由于我违反了他的三个要点中的第一个要点,这将使我损失60%的分配标记。

非常感谢您提供帮助解决此问题。在过去的几个小时中,我一直在阅读有关此问题的信息,但这对我来说似乎是一个非常特殊的情况……谢谢大家!

0 个答案:

没有答案