Shop OBEX P1 Docs P2 Docs Learn Events
Difficulties with C++ Hello World — Parallax Forums

Difficulties with C++ Hello World

doggiedocdoggiedoc Posts: 2,245
edited 2013-03-28 14:30 in Propeller 1
For reasons I can't seem recall right now I decided to try compiling a C++ program. I have the p2test build and I am compiling with SimpleIDE. I get a scope error that I don't understand.
/**
 * @file hello-cpp.cpp
 * This is the main hello-cpp program start point.
 */

#include <iostream>
#include "propeller.h"


/**
 * Main program function.
 */
int main(void)
{
    cout << "Hello, World!";
    return 0;
}


The error is:
Project Directory: /Users/doc/code/hello-cpp/

propeller-elf-c++ -o a.out -Os -mlmm -I . -fno-exceptions -fno-rtti hello-cpp.cpp
hello-cpp.cpp: In function 'int main()':
hello-cpp.cpp:14:5: error: 'cout' was not declared in this scope
hello-cpp.cpp:14:5: note: suggested alternative:
/opt/parallax/lib/gcc/propeller-elf/4.6.1/../../../../propeller-elf/include/c++/4.6.1/iostream:62:18: note:   'std::cout'
Done. Build Failed!

Click error or warning messages above to debug.

Sorry for such noob problem but I can't figure it out.

Paul

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2013-03-27 18:08
    cout lives in/under the std namespace. So either use std::cout or add a line saying that a particular namespace is used at the top of the file, e.g.
    /**
     * @file hello-cpp.cpp
     * This is the main hello-cpp program start point.
     */
    
    #include <iostream>
    
    [COLOR="#FF0000"]using namespace std;[/COLOR]
    
    /**
     * Main program function.
     */
    int main(void)
    {
        cout << "Hello, World!";
        return 0;
    }
    
  • jazzedjazzed Posts: 11,803
    edited 2013-03-27 23:56
    Once you get past the std namespace problem, you start running into memory size issues with C++. The C++ std library is a real pig. Really, you are better off without using cin, cout, etc..... Just use printf and friends.

    C++ has several great advantages over C but i'm too tired to talk about them LOL.
  • SRLMSRLM Posts: 5,045
    edited 2013-03-28 01:29
    I agree that you should use stdio.h instead of iostream, even if you are using C++. I've found that C++ on the Propeller is perfectly viable, as long as you do it more like "C with classes" than "Perfect Programming with C++".

    For reference, I've found that programs with a format specifier in the printf rarely take less than 15KB in CMM -Os mode.
  • doggiedocdoggiedoc Posts: 2,245
    edited 2013-03-28 07:44
    Thank you all for the help. Eliminating cout and [noparse]<iostream>[/noparse] and using [noparse]<stdio>[/noparse] and printf() did the trick.

    Of course then it becomes a C program although I used the c++ compile option.

    It was my intention to go through some tutorials on the web to familiarize myself with c++ but now I'm having second thoughts.

    Paul
  • jazzedjazzed Posts: 11,803
    edited 2013-03-28 07:58
    doggiedoc wrote: »
    Thank you all for the help. Eliminating cout and [noparse]<iostream>[/noparse] and using [noparse]<stdio>[/noparse] and printf() did the trick.

    Of course then it becomes a C program although I used the c++ compile option.

    It was my intention to go through some tutorials on the web to familiarize myself with c++ but now I'm having second thoughts.

    Paul

    I was talking with one of my old professors last night. We both agreed that learning C syntax before learning C++ is a good path.

    Learning C enables easily understanding C#, Java, PHP, and others. The only problem with learning C before C++ (or other object oriented languages) is in having to make the leap of understanding object methodologies in C++. Spin has some object methodologies, but not some of the more useful ideas like extending classes, class abstraction, and function overloads.
  • JasonDorieJasonDorie Posts: 1,930
    edited 2013-03-28 14:30
    doggiedoc wrote: »
    Of course then it becomes a C program although I used the c++ compile option.

    Use of the Standard Template Library doesn't define what is or isn't a C++ program.

    C++ is mostly backward compatible with C, and in the case of things like printf(), fully compatible. I've written a LOT of C++ code, and rarely use the STL, because it's a bloated pig and it's very hard to debug. :-)

    I use lots of class objects, inheritance, overloading, and all the other things C++ is good at that aren't available in C.

    If you're already familiar with C, and structures, C++ classes will be relatively easy to grasp.
Sign In or Register to comment.