Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

C++ & xCode Errors

  • 27-01-2011 11:38PM
    #1
    Registered Users, Registered Users 2 Posts: 7,994 ✭✭✭


    Hi Folks,

    Running the latest build of XCode and trying to write some C++.

    Program:
    bool isANumber(int check_prime, int max);
    
    int main () {
    	
        int check, max_N;
    
    	
        std::cout << "Enter Max N Value: ";
    	std::cin >> check;
    	
    	std::cout << check << " is the Check Value\n";
    	
    	if ( (isANumber(check, max_N) ) {
    
    //Do Something 
    }
    	
        return 0;
    }
    

    I'm very new to C++ but I've done C and Obj-C extensively. The problem is two fold:

    1) Why do I need std:: before the cin /cout commands? I'm using a command line tool project with the C++ option

    2) When I complile I get this error
    Undefined symbols:
    "isPrime(int, int)", referenced from:
    _main in main.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status

    Any pointers?


Comments

  • Registered Users, Registered Users 2 Posts: 894 ✭✭✭Dale Parish


    I havn't done C++ in years so I cannot help you with any question except for the use of std::
    Use this if you want to avoid doing that, add this to the top of your file:
    #include <iostream>
    using namespace std;
    


  • Registered Users, Registered Users 2 Posts: 981 ✭✭✭fasty


    Like in C, it's not enough to declare a function prototype, you need to provide an implementation of it.

    The std prefix is a namespace and as suggested above, you can say using namespace std; in your code to get around it.


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    ironclaw wrote: »
    Hi Folks,

    Running the latest build of XCode and trying to write some C++.

    Program:
    bool isANumber(int check_prime, int max);
    
    int main () {
    	
        int check, max_N;
    
    	
        std::cout << "Enter Max N Value: ";
    	std::cin >> check;
    	
    	std::cout << check << " is the Check Value\n";
    	
    	if ( (isANumber(check, max_N) ) {
    
    //Do Something 
    }
    	
        return 0;
    }
    

    I'm very new to C++ but I've done C and Obj-C extensively. The problem is two fold:

    1) Why do I need std:: before the cin /cout commands? I'm using a command line tool project with the C++ option

    2) When I complile I get this error



    Any pointers?
    1 ) As said previously, you must provide an implementation for a function. Computers and programming languages aren't psychic to automatically generate a function for you :)

    2 ) - Std is a namespace. It's away of avoiding conflicts. Similar groups of functions/classes are placed in a namespace. You can access these via their namespace.

    Putting
    using namespace std;
    
    will let the compiler know that you will be using functions such as cout and cin from this and allows you to implicitily mean you want the std namespace.

    However some people, including my place of work don't us the 'using' as it provides better means of avoiding conflicts. Instead use std::vector/cin etc. However this requires a bit more to write but still, shows exactly what functions you are using and from what namespace.


  • Registered Users, Registered Users 2 Posts: 7,994 ✭✭✭ironclaw


    Thanks folks. I got this sorted. With regards to the implementation of
    bool isANumber(int check_prime, int max)
    I forgot to include it in my snippet, it was declared and implemented correctly below.

    The errors disappeared, so maybe it was a error somewhere in xCode.

    Cheers for the help :)


Advertisement