1

I've searched for a solution to my problem, and the posted ones don't seem to work. I am trying to run the following in Visual Studio 2012. I have previously used Eclipse for my programming, and am adjusting to the new IDE.

    class
    IntSLLNode{
    public:
    int info;
    IntSLLNode *next;

    IntSLLNode(){
    next = 0;
    }

  IntSLLNode (int el, IntSLLNode *ptr= 0) {
  info = el;
  next = ptr;
  }

};

int main(){

#include <iostream>

using namespace std;

IntSLLNode *p = new IntSLLNode(18);
cout << *p;

return;

}

When I try to run that, it gives me an error under cout. I have included iostream and the std namespace as I normally do. Is that not correct? Can anyone assist me in getting this to work, because I quite prefer the look of the Visual Studio IDE and would like to continue using it.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
Rome_Leader
  • 2,518
  • 9
  • 42
  • 73

1 Answers1

4

The issue here is that the lines

#include <iostream>
using namespace std;

should not be inside of main. Instead, they should be at the top level of the program. Your program should look more like

#include <iostream>
using namespace std;

/* Other definitions */

int main() {
    IntSLLNode *p = new IntSLLNode(18);
    cout << *p;
}

Additionally, you have the statement

return;

inside of a function that returns an int, which isn't allowed. Try changing this to either

return 0;

or, since this is main, just skip the return all together.

Hope this helps!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • Also, there is no overloaded `operator<<` for `IntSLLNode` objects, so `cout << *p;` will still be an error. Perhaps `cout << p;`? – bcrist May 27 '13 at 21:24
  • Thanks! Isn't return; equivalent to return 0;, though? I want to output the value of the pointer p, so I have dereferenced it. Not so? – Rome_Leader May 27 '13 at 21:24
  • 5
    @user2395694 `return;` is not equivalent to `return 0;`. – juanchopanza May 27 '13 at 21:26
  • Get rid of the pointer period imo. And the `using` part can go inside `main` at least. – chris May 27 '13 at 21:27
  • @user2395694 dereferencing a pointer means getting the thing that it points to. So if by the value of the pointer you mean the address it's pointing to, you just use the pointer as is. `std::ostream` (which is the type of `std::cout`) prints out the address of pointers when you pass them using the stream insertion operator (`<<`). – bcrist May 27 '13 at 21:33
  • If you don't want the address of the node to print out, but rather some string representation of it, you need to tell the compiler what a human-readable representation of the object might look like. You do this by overloading `operator<<`. For instance: `std::ostream& operator<<(std::ostream& os, const IntSLLNode& node) { os << '(' << node.info << ' '; if (node.next) os << *node.next; os << ')'; return os; }` – bcrist May 27 '13 at 21:36
  • `using namespace std;` should rather be inside the function than outside. It doesn't need to be in / apply to global scope and to limit it to where it's necessary / useful would be better IMO. – dyp May 27 '13 at 21:48