why doesn't make work on a c++ compile with a reference address pointer?

I am a new computer science student and taking my first c++ class. I am learning a lot and this is my first post on stack exchange. I have a problem understanding what is going on with my code. I have 2 questions.

I will post my code and then ask my question.

// This program uses the address of each element in the array. 
#include <iostream>
using namespace std;

int main()
{
    const int NUM_COINS = 5;
    int coins[NUM_COINS] = {5, 1, 25, 5, 10};
    int *p1;        // Pointer to a double.
    int count;                      // Counter variable. 

    // Use the pointer to display the values in the array. 
    cout << "Here are the values in the coins array: n";
    for(count = 0; count << NUM_COINS; count++)
    {
        // Get the address of an array element
        p1 = &coins[count];

        // Display the contents of the element
        cout << *p1;
    }
    cout << endl;
    return 0;
}
  • so my first question is why doesn't make compile it? I have no problems at all with any of my other simple programs. I am using g++ on OS X 4.2.1. I have to type the g++ -o command for it to compile, if not...i get these errors:
  • g++ -c -o 9-8.o 9-8.cpp cc 9-8.o -o 9-8 Undefined symbols: "std::basic_ostream >& std::operator<<

    (std::basic_ostream >&, char const*)", referenced from: _main in 9-8.o _main in 9-8.o "std::ios_base::Init::Init()", referenced from: static_initialization_and_destruction_0(int, int)in 9-8.o
    "std::basic_string, std::allocator >::size() const", referenced from: std::__verify_grouping(char const*, unsigned long, std::basic_string, std::allocator > const&)in 9-8.o "std::basic_string, std::allocator ::operator[](unsigned long) const", referenced from: std::__verify_grouping(char const*, unsigned long, std::basic_string, std::allocator > const&)in 9-8.o std::__verify_grouping(char const*, unsigned long, std::basic_string, std::allocator > const&)in 9-8.o std::__verify_grouping(char const*, unsigned long, std::basic_string, std::allocator > const&)in 9-8.o " _gxx_personality_v0", referenced from: std::__verify_grouping(char const*, unsigned long, std::basic_string, std::allocator > const&)in 9-8.o ___tcf_0 in 9-8.o _main in 9-8.o unsigned long const& std::min(unsigned long const&, unsigned long const&)in 9-8.o __static_initialization_and_destruction_0(int, int)in 9-8.o global constructors keyed to mainin 9-8.o CIE in 9-8.o "std::ios_base::Init::~Init()", referenced from: ___tcf_ 0 in 9-8.o "std::basic_ostream >& std::endl (std::basic_ostream >&)", referenced from: _main in 9-8.o "std::basic_ostream ::operator<<(std::basic_ostream >& (*)(std::basic_ostream >&))", referenced from: _main in 9-8.o "std::basic_ostream ::operator<<(int)", referenced from: _main in 9-8.o "std::cout", referenced from: _main in 9-8.o _main in 9-8.o _main in 9-8.o ld: symbol(s) not found collect2: ld returned 1 exit status make: * [9-8] Error 1

    which leads to my second question. Even if I do type the g++ command, it compiles but after running it outputs an empty array. So my question #2 is: is my code correct? How do I properly use pointers with the reference address statement.

    Thank you.


    Reason : You are not using the comparision operator correctly. After changing it to be "<", your code should work correctly.

    for(count = 0; count << NUM_COINS; count++)
                         ^ should be "<" here
    

    I don't see any problem except that one problem in your for loop:

    for(count = 0; count << NUM_COINS; count++)
                       //^^
    

    That is not comparison. That is left-shift operation. I'm sure you didn't intend that.

    That should be : count < NUM_COINS .

    链接地址: http://www.djcxy.com/p/85068.html

    上一篇: Java使用比分配内存更多的内存

    下一篇: 为什么不使用引用地址指针进行c ++编译?