question

This is a discussion on question within the C++ Programming forums, part of the General Programming Boards category; i am getting one error when i try and compile this code, its in the last line when i try ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    57

    question

    i am getting one error when i try and compile this code, its in the last line when i try to output the date in the top node. anyone know why this is an error?

    #include <iostream.h>
    #include <fstream.h>
    #include <string>

    using namespace std;

    struct StackFrame
    {
    string data;
    StackFrame *link;

    };

    typedef StackFrame* StackFramePtr;



    void main()
    {
    char array[1][5];
    array[0][0] = 'h';
    array[0][1] = 'e';
    array[0][2] = 'l';
    array[0][3] = 'l';
    array[0][4] = 'o';
    StackFramePtr top;
    top = new StackFrame;
    top->data = array[0];
    top->link = NULL;
    cout << top->data;
    }

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    What's the error?

    Kuphryn

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    57
    something about the cout << top->data
    here it is......

    --------------------Configuration: cpp2d - Win32 Debug--------------------
    Compiling...
    cpp2d.cpp
    C:\Documents and Settings\owner\cpp2d.cpp(30) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no accepta
    ble conversion)
    Error executing cl.exe.

    cpp2d.obj - 1 error(s), 0 warning(s)

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Weird. Consider c_str().

    data.c_str()

    Kuphryn

  5. #5
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Dont know, but ditch the .h's on those header files, u used namespace.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    57
    ok getting rid of the .h worked. it outputs hello like it should but theres also some junk being output after it. any sugestions on how to get rid of this junk?

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    57
    never mind......i figured it out. i was just wondering, how come you cant use that using namespace with .h on the header files. what if i wanted to use both. i dont really understand what using namespace does

  8. #8
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    To my understanding it pulls everything into the standard namespace. Using .h is the old way of declaring, no reason to do it.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >how come you cant use that using namespace with .h on the header files.
    The old headers are typically defined like this:
    Code:
    // iostream.h
    #ifndef __IOSTREAM_H
    #define __IOSTREAM_H
    
    #include <istream.h>
    #include <ostream.h>
    
    // ...
    
    #endif
    The new ones are slightly different:
    Code:
    // iostream
    #ifndef __IOSTREAM
    #define __IOSTREAM
    
    #include <istream>
    #include <ostream>
    
    namespace std {
      // ...
    }
    
    #endif
    With the old headers you don't have to tell the compiler to look anywhere except the header itself. The new headers require you to look in the header and then in the std namespace to find what you want. The directive:
    Code:
    using namespace std;
    tells the compiler that if the name is in the std namespace, it should be accessable without resolving the namespace manually. In other words, you can say this:
    Code:
    cout<<"cout from namespace std"<<endl;
    instead of the manual namespace resolution:
    Code:
    std::cout<<"cout from namespace std"<<std::endl;
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21