我对C++有些陌生,当我尝试运行以下代码时
main.cpp:
#include <iomanip>
#include <string>
#include <limits>
#include "console.h"
using namespace std;
double calculate_future_value(double monthly_investment, double yearly_interest_rate, int years);
int main()
{
cout << "The Future Value Calculatornn";
char choice = 'y';
while (tolower(choice) == 'y')
{
cout << "INPUTn";
double monthly_investment =
console::get_double("Monthly Investment: ", 0, 10000);
double yearly_rate =
console::get_double("Yearly Interest Rate: ", 0, 30);
int years =
console::get_int("Years ", 0, 100);
cout << endl;
double future_value = calculate_future_value(monthly_investment,
yearly_rate, years);
cout << "OUTPUTn"
<< fixed << setprecision(2)
<< "Monthly Investment: " << monthly_investment << "n"
<< fixed << setprecision(1)
<< "Yearly Interest Rate: " << yearly_rate << "n"
<< "Years: " << future_value << "nn"
<< "Future Value: " << future_value << "nn";
choice = console::get_char("Continue? (y/n): ");
}
cout << "Bye!nn";
}
double calculate_future_value(double monthly_investment, double yearly_interest_rate, int years)
{
double monthly_rate = yearly_interest_rate / 12 / 100;
int months = years * 12;
double future_value = 0;
for (int i = 0; i < months; ++i)
{
future_value = (future_value + monthly_investment) *
(i + monthly_rate);
}
return future_value;
}
和
控制台.h:
#define PANDA_CONSOLE_H
#include <string>
#include <limits>
namespace console
{
double get_double(std::string prompt,
double min = std::numeric_limits<double>::min(),
double max = std::numeric_limits<double>::max());
int get_int(std::string prompt,
int min = std::numeric_limits<int>::min(),
int max = std::numeric_limits<int>::max());
char get_char(std::string prompt,
bool add_blank_line = true);
}
#endif
我会得到以下错误
Undefined symbols for architecture x86_64:
"console::get_double(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, double, double)", referenced from:
_main in main-f6ee4e.o
"console::get_int(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, int, int)", referenced from:
_main in main-f6ee4e.o
"console::get_char(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool)", referenced from:
_main in main-f6ee4e.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我已经尝试使用命名空间std将calculate_future_value((的定义移动到之后和之前的双计算未来价值(双月投资,双年利息,int年(因为这不起作用,我现在对自己需要做什么感到困惑。请帮忙。非常感谢。
您感到困惑的地方在于函数声明和函数定义之间的差异。在文件console.h
中,定义名称空间console
,并声明函数:
#ifndef PANDA_CONSOLE_H
#include <string>
#include <limits>
namespace console
{
double get_double(std::string prompt,
double min = std::numeric_limits<double>::min(),
double max = std::numeric_limits<double>::max());
int get_int(std::string prompt,
int min = std::numeric_limits<int>::min(),
int max = std::numeric_limits<int>::max());
char get_char(std::string prompt,
bool add_blank_line = true);
}
#endif
(注意:您的头保护应该是#ifndef PANDA_CONSOLE_H
,这样,如果尚未定义PANDA_CONSOLE_H
,则会包含以下信息。[但是,如果您在多个源文件中包含console.h
,请考虑包含一个头保护,以防止函数声明多次包含](
对于每个函数声明——必须有相应的函数定义
当您声明一个函数时,您只是简单地告诉编译,稍后将定义具有此名称(符号(的函数,但您可以从该源文件中的声明点开始使用该函数。
当定义函数时,编译器会为其保留空间,并提供找到该函数的编译代码的地址。在链接过程中,无论何时在源文件中使用该函数,它都将解析为该函数的编译代码实际所在的地址。
如果声明函数,但未能定义该函数,则当链接器试图定位与声明相关的代码时,它找不到它,并将发出类似于以下的错误:
Undefined symbols for (insert missing function name here)
在您的情况下,您需要为consile.h
中声明的每个函数编写一个包含定义的console.cpp
。例如(这只是一个没有努力使函数与main()
中的函数完全匹配的例子(,您可以编写以下console.cpp
来定义每个函数,例如
#include "console.h"
#include <iostream>
double console::get_double(std::string prompt, double min, double max)
{
double value = 0;
std::cout << prompt;
if (!(std::cin >> value)) {
std::cerr << "error: invalid double value or EOF.n";
return 0;
}
if (value < min || max < value) {
std::cerr << "error: value out of range.n";
return 0;
}
return value;
}
int console::get_int(std::string prompt, int min, int max)
{
int value = 0;
std::cout << prompt;
if (!(std::cin >> value)) {
std::cerr << "error: invalid integer value or EOF.n";
return 0;
}
if (value < min || max < value) {
std::cerr << "error: value out of range.n";
return 0;
}
return value;
}
char console::get_char(std::string prompt, bool add_blank_line)
{
char value = 0;
std::cout << prompt;
if (!(std::cin >> value)) {
std::cerr << "error: EOF or unrecoverable errorn";
return 0;
}
if (add_blank_line)
std::cout.put('n');
return value;
}
(注意:console.cpp
中的#include <iostream>
仅用于std::cin
、std::cout
和std::cerr
调用(
编译
然后你可以用编译你的项目
$ g++ -Wall -Wextra -pedantic -Wshadow -std=c++11 -Ofast console.cpp -o main main.cpp
or
$ clang++ -Wall -Wextra -pedantic -Wshadow -std=c++11 -Ofast console.cpp -o main main.cpp
然后你可以运行你的./main
程序(根据我编造的函数定义,它会产生非常错误的输出(
示例使用/输出
$ ./main
The Future Value Calculator
INPUT
Monthly Investment: 123.45
Yearly Interest Rate: 12.4
Years 4
OUTPUT
Monthly Investment: 123.45
Yearly Interest Rate: 12.4
Years: 90457540156111170020897362009600300274844235338297210119913472.0
Future Value: 90457540156111170020897362009600300274844235338297210119913472.0
Continue? (y/n): n
Bye!
注意:我建议您将OUTPUT
中的yearly_rate
更改为years
,以便正确显示年份:
<< "Years: " << years << "nn"
关键是,对于每个函数声明,都必须有一个相应的函数definition以允许链接器将代码中使用的符号与该函数的实际代码相匹配。让我知道这是否填补了缺失的部分,如果没有,我很乐意提供进一步的帮助。
(如果你支持你的计划计算的支出,我有123.45美元可投资(