C++ 未正确调用运算符溢出函数调用

C++ 未正确调用运算符溢出函数调用,c++,compiler-errors,lnk2001,C++,Compiler Errors,Lnk2001,所以,我得到一个错误,在VS 2012中的错误C4930,说 prototyped function not called (was a variable definition intended?) 基本上,由于某些原因,我使用运算符溢出函数进行的所有函数调用都没有被正确调用。我见过一些人这样写函数调用 Comp_Num Comp_Num::operator- (Comp_Num &c1, Comp_Num &c2); 当我添加Comp_Num::VS下划线操作符中的文本编辑

所以,我得到一个错误,在VS 2012中的错误C4930,说

prototyped function not called (was a variable definition intended?)
基本上,由于某些原因,我使用运算符溢出函数进行的所有函数调用都没有被正确调用。我见过一些人这样写函数调用

Comp_Num Comp_Num::operator- (Comp_Num &c1, Comp_Num &c2);
当我添加Comp_Num::VS下划线操作符中的文本编辑器时,会出现红色的波形

我也得到3链接2001错误。我为双r1\u final和r2\u final所做的变量声明有问题。我认为他们没有在函数调用之外初始化变量。这总共是一条错误消息

c:\users\andrew\documents\visual studio 2012\projects\andrew_spiteri_hw5\complex_number.h(226): warning C4930: 'Comp_Num operator +(Comp_Num &,Comp_Num &)': prototyped function not called (was a variable definition intended?)
c:\users\andrew\documents\visual studio 2012\projects\andrew_spiteri_hw5\complex_number.h(228): warning C4930: 'Comp_Num operator -(Comp_Num &,Comp_Num &)': prototyped function not called (was a variable definition intended?)
c:\users\andrew\documents\visual studio 2012\projects\andrew_spiteri_hw5\complex_number.h(230): warning C4930: 'Comp_Num operator *(Comp_Num &,Comp_Num &)': prototyped function not called (was a variable definition intended?)
Complex_Number.obj : error LNK2001: unresolved external symbol "private: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > Comp_Num::imag1" (?imag1@Comp_Num@@0V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A)
Complex_Number.obj : error LNK2001: unresolved external symbol "public: static double Comp_Num::r1_final" (?r1_final@Comp_Num@@2NA)
Complex_Number.obj : error LNK2001: unresolved external symbol "public: static double Comp_Num::r2_final" (?r2_final@Comp_Num@@2NA)
c:\users\andrew\documents\visual studio 2012\projects\andrew_spiteri_hw5\complex_number.h(226):警告C4930:'Comp_Num operator+(Comp_Num&,Comp_Num&'):未调用原型函数(是否使用变量定义?)
c:\users\andrew\documents\visual studio 2012\projects\andrew_spiteri_hw5\complex_number.h(228):警告C4930:“Comp_Num操作符-(Comp_Num&,Comp_Num&)':未调用原型函数(是否使用变量定义?)
c:\users\andrew\documents\visual studio 2012\projects\andrew_spiteri_hw5\complex_number.h(230):警告C4930:“Comp_Num operator*(Comp_Num&,Comp_Num&)”:未调用原型函数(是否使用变量定义?)
Complex_Number.obj:错误LNK2001:未解析的外部符号“private:static class std::basic_string Comp_Num::imag1”(?imag1@Comp_Num@@0V?$basic_string@DU?$char_traits@D@性病病毒$allocator@D@2@@std@@A)
Complex_Number.obj:错误LNK2001:未解析的外部符号“public:静态双Comp_Num::r1_final”(?r1_final@Comp_Num@@2NA)
Complex_Number.obj:错误LNK2001:未解析的外部符号“public:static double Comp_Num::r2_final”(?r2_final@Comp_Num@@2NA)
这是密码。我要说的部分是operation()函数的底部。提前谢谢

#include <iostream>
#include <String>
#include <vector>
#include <sstream>

using namespace std;

#ifndef Comp_Num
class Comp_Num
{
private:
    std::vector <double> real;
    std::vector <double> imag;
    double real_in, real_in1, real_in2;
    static double r1_final, r2_final;
    std::string imag_in; 
    static std::string imag1;
public:
    Comp_Num();
    Comp_Num(double real_num, std::string img_num);

    static std::vector <std::string> get_input();
    static std::vector <std::string> get_real(std::vector <string> complexnum1);
    static std::vector <std::string> get_imag(std::vector <std::string> complexnum2);
    static void display(std::vector <std::string> display1);
    static void display2(std::vector <double> display3);
    static std::vector <std::string> set_compnum();
    static std::vector <std::string> comp2real(std::vector <std::string> c2r);
    static std::vector <double> real2double (std::vector<std::string> r2d);
    static double double_const(std::vector<double> dc);
    friend Comp_Num operator +(const Comp_Num &c3, const Comp_Num &c4);
    friend Comp_Num operator -(const Comp_Num &c3, const Comp_Num &c4);
    friend Comp_Num operator *(const Comp_Num &c3, const Comp_Num &c4);
    static void operation();
    ~Comp_Num();
/*
    double Comp_Num::operator+ (double add_num);
    double Comp_Num::operator- (double sub_num);
    double Comp_Num::operator* (double mul_num);
    double Comp_Num::operator/ (double div_num);
*/
};
#endif !Comp_Num

Comp_Num::Comp_Num()
{
    double real_in = 1;
    string imag_in = "i";
};

Comp_Num::Comp_Num(double real_num, std::string imag_num)
{
    double real_in = real_num;
    string imag_in = imag_num;
};

std::vector <string> Comp_Num::set_compnum()
{
    std::vector <string>set_num= Comp_Num::get_input();
    std::vector<string>comp1=Comp_Num::get_real(set_num);
    std::vector <string>comp2= Comp_Num::get_imag(set_num);
    std::vector <string>cmp2rl_fin = Comp_Num::comp2real(comp2);
    std::vector<double>finale = Comp_Num::real2double(cmp2rl_fin);
    Comp_Num::double_const (finale);
    imag1 = "i";
    Comp_Num *c1 = new Comp_Num(r1_final, imag1);
    Comp_Num *c2 = new Comp_Num(r2_final, imag1);

    Comp_Num::display(comp2);
    std::system("pause");
    return(comp2);
};

std::vector <string> Comp_Num::get_input()
{
    std::string usr_input, input1;
    std::vector <string> complex;
    std::cout<<"Enter 2 imaginary numbers you would like to perform your operation on.\n";
    std::getline(std::cin, usr_input);
    std::istringstream input(usr_input);
    while(input >> input1)
    {
        complex.push_back(input1);
    }
    std::cout<<'\n';
    return (complex);
};

std::vector <string> Comp_Num::get_imag(std::vector <string> complexnum2)
{
    std::vector <string> imag1;
    for(unsigned int i = 0; i < complexnum2.size(); ++i)
    {
        std::string str2 = "";
        std::vector<char> char1(complexnum2[i].begin(), complexnum2[i].end());
        for(unsigned int r = 0; r < char1.size(); ++r)
        {
            if(char1[r] >= '0' && char1[r] <= '9' || char1[r] == '-' || char1[r] == '.' || char1[r] == 'i')
            {
                str2 += char1[r];
                if(char1[r] == 'i')
                {
                    imag1.push_back(str2);  
                    continue;
                }
            }
        }
    }
    return(imag1);
};

std::vector <string> Comp_Num::get_real(std::vector <string> complexnum1)
{
    std::vector <string> real1;
    for(unsigned int i = 0; i < complexnum1.size(); ++i)
    {
        std::string str2 = "";
        std::vector<char> char1(complexnum1[i].begin(), complexnum1[i].end());
        for(unsigned int r = 0; r < char1.size(); ++r)
        {
            if(char1[r] >= '0' && char1[r] <= '9' || char1[r] == '-' || char1[r] == '.' || char1[r] == 'i')
            {
                str2 += char1[r];
                if(char1[r] == 'i')
                {
                    str2="";
                    break;
                }
            }
        }
        real1.push_back(str2);
    }
    return(real1);
};

void Comp_Num::display (std::vector <string> display1)
{
    unsigned int i = 0;
    while (i < display1.size())
    {
        std::cout<<display1[i]<<" ";
        i++;
    }
    std::cout<<std::endl;
};

void Comp_Num::display2 (std::vector <double> display3)
{
    unsigned int i = 0;
    while (i < display3.size())
    {
        std::cout<<display3[i]<<" ";
        i++;
    }
    std::cout<<std::endl;
};

std::vector <string> Comp_Num::comp2real(std::vector <string> c2r)
{
    std::vector <string> real2;
    for(unsigned int i = 0; i < c2r.size(); ++i)
    {
        std::string str2 = "";
        std::vector<char> char1(c2r[i].begin(), c2r[i].end());
        for(unsigned int r = 0; r < char1.size(); ++r)
        {
            if(char1[r] >= '0' && char1[r] <= '9' || char1[r] == '-' || char1[r] == '.')
            {
                str2 += char1[r];
            }
        }
        real2.push_back(str2);
    }
    return(real2);
}

std::vector<double> Comp_Num::real2double (std::vector<string> r2d)
{
    double y;
    std::vector<double> final_r2d;
    std::string str, str2;
    std::vector<string>str3;
    for(unsigned int i = 0; i < r2d.size(); ++i)
    {
        std::vector<char> ch(r2d[i].begin(), r2d[i].end());
        for(unsigned int r = 0; r < ch.size(); ++r)
        {
            if(ch[r] >= '0' && ch[r] <= '9' || ch[r] == '-' || ch[r] == '.')
            {
                str2 += ch[r];  
            }
        }
        str3.push_back(str2);
        str2="";
    }
    for(unsigned int r = 0; r < str3.size(); r++)
    {
        str = str3[r];
        std::istringstream to_double(str);
        while(to_double >> y)
        {
            final_r2d.push_back(y);
        }
    }
    return(final_r2d);
};

double Comp_Num::double_const(std::vector<double>dc)
{
    r1_final=dc[0];
    r2_final=dc[1];
    return(0);
};

void Comp_Num::operation()
{
    std::string operate;
    std::cout<<"What operation would you like perform on the numbers?\n";
    std::cout<<"You may choose between addition, subtraction, and multiplication.\n";
    std::getline(cin,operate);
    if(operate == "addition")
        Comp_Num operator+ (Comp_Num &c1, Comp_Num &c2);
    else if (operate == "subtraction")
        Comp_Num operator- (Comp_Num &c1, Comp_Num &c2);
    else if (operate == "multiplication")
        Comp_Num operator* (Comp_Num &c1, Comp_Num &c2);
    else
    {
        cout<<"This is not a valid entry.  Please start over."<<std::endl;
        std::exit(0);
    }
};

Comp_Num operator +(const Comp_Num &c3, const Comp_Num &c4)
{
    std::string output,imaginary = "i";
    double final_real = c3.real_in + c4.real_in;
    std::cout<<"The difference of your imaginary numbers is "<<final_real<<imaginary<<std::endl;
    return(c3);
};

Comp_Num operator -(const Comp_Num &c3, const Comp_Num &c4)
{
    std::string output,imaginary = "i";
    double final_real = c3.real_in - c4.real_in;
    std::cout<<"The difference of your imaginary numbers is "<<final_real<<imaginary<<std::endl;
    return(c3);
};

Comp_Num operator *(const Comp_Num &c3, const Comp_Num &c4)
{
    std::string output,imaginary = "i";
    double final_real = c3.real_in * c4.real_in;
    final_real *= -1;
    std::cout<<"The difference of your imaginary numbers is "<<final_real<<std::endl;
    return(c3);
};
#包括
#包括
#包括
#包括
使用名称空间std;
#ifndef Comp_Num
类Comp_Num
{
私人:
向量实数;
std::向量imag;
双实数进位,实数进位1,实数进位2;
静态双r1_-final,r2_-final;
std::字符串imag_in;
静态std::字符串imag1;
公众:
Comp_Num();
Comp_Num(双实数,std::string img_Num);
静态std::vector get_input();
静态std::vector get_real(std::vector complexnum1);
静态std::vector get_imag(std::vector complexnum2);
静态无效显示(标准::矢量显示1);
静态无效显示2(标准::矢量显示3);
静态std::向量集_compnum();
静态std::vector comp2real(std::vector c2r);
静态std::vector real2double(std::vector r2d);
静态双双常数(std::vector dc);
友元复合数运算符+(常数复合数和c3,常数复合数和c4);
友元复合数运算符-(常数复合数和c3,常数复合数和c4);
友元复合数运算符*(常数复合数和c3,常数复合数和c4);
静态无效操作();
~Comp_Num();
/*
double Comp_Num::operator+(double add_Num);
double Comp_Num::运算符-(double sub_Num);
双复数::运算符*(双复数);
双复数:运算符/(双复数);
*/
};
#恩迪夫!补偿数
Comp_Num::Comp_Num()
{
双实数_in=1;
字符串imag_in=“i”;
};
Comp_Num::Comp_Num(双实数,标准::字符串imag_Num)
{
双实数=实数;
字符串imag_in=imag_num;
};
std::vector Comp_Num::set_compnum()
{
std::vector set_num=Comp_num::get_input();
std::vectorcomp1=Comp_Num::get_real(set_Num);
std::vector comp2=Comp_Num::get_imag(set_Num);
std::vector cmp2rl_fin=Comp_Num::comp2real(comp2);
std::vectorfinale=Comp_Num::real2double(cmp2rl_fin);
Comp_Num::double_const(终曲);
imag1=“i”;
Comp_Num*c1=新Comp_Num(r1_final,imag1);
Comp_Num*c2=新Comp_Num(r2_final,imag1);
Comp_Num::显示(comp2);
标准::系统(“暂停”);
返回(comp2);
};
std::vector Comp_Num::get_input()
{
std::字符串usr_输入,input1;
std::向量复合体;
标准::cout输入1)
{
复杂。推回(输入1);
}

std::cout当您调用这些方法时,您将返回一个Comp_Num对象

   Comp_Num cm 
   if(operate == "addition")
   cm = c1 + c2;
   else if (operate == "subtraction")
   cm = c1 - c2;
   else if (operate == "multiplication")
   cm = c1 * c2;

以下是问题所在:

if(operate == "addition")
    Comp_Num operator+ (Comp_Num &c1, Comp_Num &c2);
else if (operate == "subtraction")
    Comp_Num operator- (Comp_Num &c1, Comp_Num &c2);
else if (operate == "multiplication")
    Comp_Num operator* (Comp_Num &c1, Comp_Num &c2);
如果要使用运算符,只需将它们用作运算符即可

Comp_num c1, c2, c3; // get these from somewhere.
if(operate == "addition")
    c3 = c1 + c2;
else if (operate == "subtraction")
    c3 = c1 - c2;
else if (operate == "multiplication")
    c3 = c1 * c2;
另一个错误是因为您需要在类之外定义静态变量。您也可以在此时初始化它们。请在类定义下面尝试类似的操作:

double Comp_Num::r1_final = 0;