Menu

problems with std

2008-09-27
2012-09-26
  • Richard Kopcke

    Richard Kopcke - 2008-09-27

    I am running 4.9.9.2 with XP
    The errors for the following program seem to be related to not finding features of namespace std.
    The following runs well if all parts of the project are combined into one .cpp
    My experience with these logs is not adequate to find the remedy.

    The compile log -- main -- mySeq.hpp -- mySeq.cpp follow.

    Thank you.
    Dick

    Compiler: Default compiler
    Building Makefile: "C:\CPP\test\Makefile.win"
    Executing make...
    make.exe -f "C:\CPP\test\Makefile.win" all
    g++.exe -c mySeq.cpp -o mySeq.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"

    In file included from mySeq.cpp:1:
    mySeq.hpp:7: error: NULL' was not declared in this scope mySeq.hpp:12: error:NULL' was not declared in this scope

    mySeq.hpp: In destructor mySeq::~mySeq()': mySeq.hpp:8: error:NULL' undeclared (first use this function)
    mySeq.hpp:8: error: (Each undeclared identifier is reported only once for each function it appears in.)

    mySeq.cpp: In constructor mySeq::mySeq(int, const double*)': mySeq.cpp:7: error:NULL' undeclared (first use this function)

    mySeq.cpp: In member function void mySeq::writeSeq() const': mySeq.cpp:24: error:cout' is not a member of std' mySeq.cpp:26: error:cout' is not a member of `std'

    make.exe: *** [mySeq.o] Error 1

    Execution terminated

    include <cstdlib>

    include <iostream>

    include <cmath>

    include "mySeq.hpp"

    int main(int argc, char *argv[])
    {
    int ndefault=2;
    double d[] = {1., 1.};

    mySeq::setDefault(ndefault, d);
    
    std::cout &lt;&lt; &quot;defaultList&quot; &lt;&lt; '\n' &lt;&lt; '\n';
    mySeq::writeDefault();
    
    system(&quot;PAUSE&quot;);
    return EXIT_SUCCESS;
    

    }

    ifndef MYSEQ_HPP

    define MYSEQ_HPP

    class mySeq
    {
    public:
    mySeq(const int n=0, const double* const d=NULL);
    ~mySeq() { delete [] data; data = NULL; }

       void writeSeq() const;
    
       static void setDefault(const int=0, const double* const d=NULL);
       static void writeDefault() { defaultSeq.writeSeq(); }
    

    private:
    int size;
    double* data;
    static mySeq defaultSeq;
    };

    mySeq mySeq::defaultSeq;

    endif

    include "mySeq.hpp"

    mySeq::mySeq(const int n, const double* const d)
    {
    size = n ? n : mySeq::defaultSeq.size;
    data = new double [size];
    if (d==NULL)
    {
    for (int i=0; i<size; i++) data[i] = mySeq::defaultSeq.data[i];
    }
    else
    {
    for (int i=0; i<size; i++) data[i] = d[i];
    }
    }

    void mySeq::setDefault(const int sz, const double* const d)
    {mySeq::defaultSeq = mySeq(sz, d);}

    void mySeq::writeSeq() const
    {
    for (int i=0; i<size; i++)
    {
    std::cout << i << '\t' << data[i] << '\n';
    }
    std::cout << '\n';
    }

     
    • cpns

      cpns - 2008-09-27

      > mySeq.hpp:7: error: `NULL' was not declared in this scope

      NULL is a macro, and requires definition. Various headers in the standard library define it but you have included none. The solution however is not to include a definition or a defining header but rater not to use it at all, ever. Forget you ever even heard of NULL, and just use 0 (plain zero literal constant). That is the recommendation of Bjarne Stroustrup; zero is always a valid assignment to any built in data type including pointers, and so does not require a macro definition and cannot be rendered incorrect by a bad redefinition of NULL. In C++ NULL is correctly defined as 0, in C however it is usually (void*)NULL which will fail in C++ because of stronger type checking.

      > mySeq.cpp:24: error: cout' is not a member ofstd'

      In this file you included only "mySeq.hpp" which includes nothing else. Consequently you have not included the header that declared std::cout (<iostream>). You included it in main.cpp, but that is not sufficient; each source file is independently compiled (if you look at the build log after a Rebuild all, you will see a separate compiler command line for each module, then - if successful - a final link stage that combines the modules and resolves interdependencies). Because of this separate compilation, each module must be independently valid, which means declarations are required before use of all symbols referenced.

      Clifford

       
      • Richard Kopcke

        Richard Kopcke - 2008-09-27

        Of course! Really dumb error. And, I really must buy a laptop with a bigger screen.

        Thanks again
        Dick

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.