通过参考程序c ++

时间:2017-11-04 00:08:13

标签: c++

我只编写了几个月的编码,而且我遇到了其中一项任务的障碍。描述是

  

“重写带有函数的钱包porgram执行插入和   删除操作。函数初始化(int& p,int& n,int& d,   INT和放大器; q)将便士,镍币,硬币和零件初始化为零。   函数insert(int& p,int& n,int& d,int& q)将增加便士,   镍,硬币和四分之一。函数dollars()将返回   美元。函数remove(int& p,int& n,int& d,int& q)将   减去便士,镍币,硬币和四分之一。功能显示()   返回一个新的String以打印剩余的钱包内容   便士,镍币,角钱和宿舍。“

这是我迄今为止所能提出的。

#include <iostream>

using namespace std;

int main()
{
   int pennies, nickels, dimes, quarters, p, n, d, q;
   float total;

   void initialize(int&, int&, int&, int&);
   void insert(int&, int&, int&, int&);
   void remove(int&, int&, int&, int&);
   float dollars(int, int, int, int);
   void print(int, int, int, int, float);


   initialize(pennies, nickels, dimes, quarters);
   total = dollars(pennies, nickels, dimes, quarters);
   print(pennies, nickels, dimes, quarters, total);

   insert(pennies, nickels, dimes, quarters);
   total = dollars(pennies, nickels, dimes, quarters);
   print(pennies, nickels, dimes, quarters, total);

   remove(pennies, nickels, dimes, quarters);
   total = dollars(pennies, nickels, dimes, quarters);
   print(pennies, nickels, dimes, quarters, total);

}
void initialize(int& pennies, int& nickels, int& dimes, int& quarters)

{
    pennies=0;
    nickels=0;
    dimes=0;
    quarters=0;
}

void insert(int& pennies, int& nickels, int& dimes, int& quarters)
{
    int p, n, d, q;
    cout << "Insert pennies, nickels, dimes, and quarters:";
    cin >> p, n, d, q;

    pennies +=p;
    nickels +=n;
    dimes +=d;
    quarters +=q;

}

void remove(int& pennies, int& nickels, int& dimes, int& quarters)
{
    int p, n, d, q;
    cout << "Remove pennies, nickels, dimes, and quarters:";
    cin >> p, n, d, q;

    pennies -=p;
    nickels -=n;
    dimes -=d;
    quarters -=q;

}

float dollars(int pennies, int nickels, int dimes, int quarters, float total)
{
    total= pennies + 5*nickels + 10*dimes + 25*quarters;
    total=(float)total/100;

    return total;
}
void print(int pennies, int nickels, int dimes, int quarters, float total)
{
    cout << quarters << "quarters+ " << dimes << " dimes+" << nickels << " nickels+" << pennies << " pennies= $" << total <<"\n";

}

我一直在使用onlinegdb.com来编译我的所有程序,因为我的所有课程都要求我使用。当我编译这个程序时,我得到了

/tmp/ccd19JQt.o: In function `main':
main.cpp:(.text.startup+0x2b): undefined reference to `dollars(int, int, int, int)'
main.cpp:(.text.startup+0x6a): undefined reference to `dollars(int, int, int, int)'
main.cpp:(.text.startup+0xa9): undefined reference to `dollars(int, int, int, int)'
collect2: error: ld returned 1 exit status

有没有人知道我可以做些什么来让这个程序编译和运行?非常感谢你。

这是我改变它以使其工作的方式。谢谢你的建议。

int main()
{
   int pennies, nickels, dimes, quarters, p, n, d, q;
   float total;

   void initialize(int&, int&, int&, int&);
   void insert(int&, int&, int&, int&);
   void remove(int&, int&, int&, int&);
   float dollars(int, int, int, int);
   void print(int, int, int, int, float);


   initialize(pennies, nickels, dimes, quarters);
   total = dollars(pennies, nickels, dimes, quarters);
   print(pennies, nickels, dimes, quarters, total);

   insert(pennies, nickels, dimes, quarters);
   total = dollars(pennies, nickels, dimes, quarters);
   print(pennies, nickels, dimes, quarters, total);

   remove(pennies, nickels, dimes, quarters);
   total = dollars(pennies, nickels, dimes, quarters);
   print(pennies, nickels, dimes, quarters, total);

}
void initialize(int& pennies, int& nickels, int& dimes, int& quarters)

{
    pennies=0;
    nickels=0;
    dimes=0;
    quarters=0;
}

void insert(int& pennies, int& nickels, int& dimes, int& quarters)
{
    int p, n, d, q;
    cout << "Insert pennies, nickels, dimes, and quarters:";
    cin >> p>>n>>d>>q;

    pennies +=p;
    nickels +=n;
    dimes +=d;
    quarters +=q;

}

void remove(int& pennies, int& nickels, int& dimes, int& quarters)
{
    int p, n, d, q;
    cout << "Remove pennies, nickels, dimes, and quarters:";
    cin >> p>>n>>d>>q;

    pennies -=p;
    nickels -=n;
    dimes -=d;
    quarters -=q;

}

float dollars(int pennies, int nickels, int dimes, int quarters)
{
    float total;
    total= pennies + 5*nickels + 10*dimes + 25*quarters;
    total=(float)total/100;

    return total;
}
void print(int pennies, int nickels, int dimes, int quarters, float total)
{
    cout << quarters << "quarters+ " << dimes << " dimes+" << nickels << " nickels+" << pennies << " pennies= $" << total <<"\n";

}

1 个答案:

答案 0 :(得分:0)

以下是声明:

In file included from experiment/miniso.cpp:1:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/c++/7.2.0/vector:61:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/c++/7.2.0/bits/allocator.h:46:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/x86_64-linux-gnu/c++/7.2.0/bits/c++allocator.h:33:
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/c++/7.2.0/ext/new_allocator.h:136:23: error: call to deleted constructor of 'Foo'
        { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
                             ^   ~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/c++/7.2.0/bits/alloc_traits.h:475:8: note: in instantiation of function template
      specialization '__gnu_cxx::new_allocator<Foo>::construct<Foo, Foo>' requested here
        { __a.construct(__p, std::forward<_Args>(__args)...); }
              ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/c++/7.2.0/bits/vector.tcc:100:21: note: in instantiation of function template
      specialization 'std::allocator_traits<std::allocator<Foo> >::construct<Foo, Foo>' requested here
            _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
                           ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.2.0/../../../../include/c++/7.2.0/bits/stl_vector.h:954:9: note: in instantiation of function template
      specialization 'std::vector<Foo, std::allocator<Foo> >::emplace_back<Foo>' requested here
      { emplace_back(std::move(__x)); }
        ^
experiment/miniso.cpp:24:5: note: in instantiation of member function 'std::vector<Foo, std::allocator<Foo> >::push_back' requested here
  v.push_back(Foo());
    ^
experiment/miniso.cpp:19:3: note: 'Foo' has been explicitly marked deleted here
  Foo(const Foo &) = delete;
  ^

您已宣布了4个参数。

这是函数调用:

float dollars(int, int, int, int);
//            1    2    3    4

你传递了4个参数。到目前为止一切都很好。

这是定义:

total = dollars(pennies, nickels, dimes, quarters);
//              1        2        3      4

您已经为5参数函数float dollars(int pennies, int nickels, int dimes, int quarters, float total) // 1 2 3 4 5 { ... 定义了一个定义。链接器错误是由于缺少您调用的dollars(int,int,int,int,float)的定义,但没有定义。

解决方案:定义函数dollars(int,int,int,int)