我是一名计算机科学专业的学生,正在参加我的第一堂 C++ 课程。我无法理解我的代码发生了什么:
// 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;
}
- 所以我的第一个问题是为什么 make 不编译它?我对其他任何简单程序都没有任何问题。我在 OS X 4.2.1 上使用 g++。我必须输入 g++ -o 命令才能编译,如果没有......我会收到以下错误:
g++ -c -o 9-8.o 9-8.cpp cc 9-8.o
-o 9-8 未定义的符号:“std::basic_ostream >&
std::运算符<<
<块引用>
(std::basic_ostream >&, 字符
const*)”,引用自:
_main 在 9-8.o
9-8.o 中的_main“std::ios_base::Init::Init()”,
参考自:
9-8.o 中的 __static_initialization_and_destruction_0(int, int)
“std::basic_string,
std::allocator >::size() const",
参考自:
std::__verify_grouping(char const*, 无符号长整型,
std::basic_string,
std::分配器> const&) 在 9-8.o
“std::basic_string,
std::分配器
::operator[](unsigned long) const",引用自:
std::__verify_grouping(char const*, 无符号长整型,
std::basic_string,
std::分配器> const&) 在 9-8.o
std::__verify_grouping(char const*, 无符号长整型,
std::basic_string,
std::分配器> const&) 在 9-8.o
std::__verify_grouping(char const*, 无符号长整型,
std::basic_string,
std::分配器> const&) 在 9-8.o
“___gxx_personality_v0”,引用
从:
std::__verify_grouping(char const*, 无符号长整型,
std::basic_string,
std::分配器> const&) 在 9-8.o
9-8.o 中的 ___tcf_0
_main 在 9-8.o
无符号长常量& std::min(无符号长整型
const&、unsigned long const&) 在 9-8.o 中
9-8.o 中的 __static_initialization_and_destruction_0(int, int)
mainin 9-8.o 的全局构造函数
CIE 9-8.o“std::ios_base::Init::~Init()”,
参考自:
9-8.o 中的 ___tcf_0 “std::basic_ostream >&
标准::endl
(std::basic_ostream >&)",
参考自:
9-8.o 中的 _main “std::basic_ostream
::operator<<(std::basic_ostream >&
(*)(std::basic_ostream >&))",
参考自:
9-8.o 中的 _main “std::basic_ostream
::operator<<(int)",引用自:
_main 在 9-8.o "std::cout" 中,引用自:
_main 在 9-8.o
_main 在 9-8.o
9-8.o ld 中的_main:未找到符号collect2:ld 返回 1 退出
状态 make: *** [9-8] 错误 1
这导致了我的第二个问题。即使我输入 g++ 命令,它也会编译,但运行后会输出一个空数组。所以我的问题#2 是:我的代码正确吗?如何在引用地址语句中正确使用指针?
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;
}
- 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?
发布评论
评论(2)
原因:您没有正确使用比较运算符。将其更改为“<”后,您的代码应该可以正常工作。
Reason: You are not using the comparision operator correctly. After changing it to be "<", your code should work correctly.
除了
for
循环中的一个问题之外,我没有看到任何问题:这不是比较。即左移运算。我确信你不是故意的。
那应该是:
count < NUM_COINS
。I don't see any problem except that one problem in your
for
loop:That is not comparison. That is left-shift operation. I'm sure you didn't intend that.
That should be :
count < NUM_COINS
.