简体   繁体   中英

Why doesn't make work on a C++ compile with a reference address pointer?

I am a computer science student and taking my first C++ class. I have a problem understanding what is going on with my code:

// 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;
}
  1. 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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM