无法在 main 中声明嵌套类的即时。"对 Class::NestedClass::NestedClass() 的未定义引用;

Can't declare instant of nested class in main. "undefined reference to Class::NestedClass::NestedClass();

本文关键字:NestedClass Class 引用 未定义 main 声明 嵌套      更新时间:2023-10-16

我认为我的头文件有一些简单的错误,我有嵌套类声明,如果不是那样,我一定错过了设置嵌套类的一个步骤。当我试图声明它的一个实例(printpersonal)时,我只会在main中得到一个错误。

main中的错误直到底部才出现。以上都很好。

#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <stdio.h>
#include "Stocks.h"
using namespace std;
int main(){
cout<<"Create account username: "<<endl;
string name;
cin>>name;
cout<<"Create password: "<<endl;
string password;
cin>>password;
accountinfo personal;
personal.name=name;
personal.password=password;
ifstream data;
int counter=0;
dividends div;
data.open("Netflix.csv");
string word;
Stocks annualdata;//class
//Date, Open, High, Low, Close, Volume, Adjusted Close
ticker info;//struct
int lineindex=0;
while(getline(data, word, 'n')){
        stringstream ss;
        int wordindex=0;
        if(lineindex>0){
        ss<<word;
        while(getline(ss,word,',')){
            if(wordindex==0){//10 lines of data total
                info.date=word;
                }
             if(wordindex==1){//10 lines of data total
                info.open=addDouble(word);
                }
             if(wordindex==2){//10 lines of data total
                info.high=addDouble(word);
                }
             if(wordindex==3){//10 lines of data total
                info.low=addDouble(word);
                }
             if(wordindex==4){//10 lines of data total
                info.close=addDouble(word);
                }
             if(wordindex==5){//10 lines of data total
                info.volume=addInt(word);
                }
             if(wordindex==6){//10 lines of data total
                info.adjustedclose=addDouble(word);
                }
        wordindex++;
            }
        }
    annualdata.addDay(info);
    lineindex++;
}
annualdata.maxprofit();
annualdata.oneyear();
annualdata.sixmonths();
annualdata.maxloss();
double money;
double divs=div.annualdiv;
if(data=="Apple.csv"){
    money=div.addDiv(divs);
    div.oneyear();
    }
Stocks::Account printpersonal;
printpersonal.printaccountinfo(personal);

return 0;
}
头文件

#ifndef STOCKS_H
#define STOCKS_H
#include <string>
using namespace std;
 //Date, Open, High, Low, Close, Volume, Adjusted Close
 struct ticker{
 std::string date;
 double open;
 double high;
 double low;
 double close;
 int volume;
 double adjustedclose;
 };
 struct accountinfo{
std::string name;
std::string password;
};
class Stocks//base class
{
public:
    Stocks();//Constructor1 method7
    Stocks(std::string, double, double, double, double, int, double);
    void addDay(ticker);//method1
    void maxprofit();//method2
    void oneyear();//method3
    void sixmonths();//method4
    void maxloss();//method5

    int index=253;
    double money=1000;
    int shares;
    ticker annual[254];
    class Account {//nested class
    public:
    Account();//method11
    void printaccountinfo(accountinfo info){//method12
    }
};

protected:
private:
};
class dividends:public Stocks{//derived class
public:
    dividends(){};//constructor2 method8
    double annualdiv=(.57+.57+.52+.52);
 double addDiv(double annualdiv);//method6
 void oneyear();//overloaded method method9

 };
#endif // STOCKS_H
源文件

#include "Stocks.h"
#include <iostream>
using namespace std;
 Stocks::Stocks(string d, double o, double h, double l, double c, int   v, double ac)
 {
 string date=d;
 double open=o;
double high=h;
double low=l;
int volume=v;
 double adjustedclose=ac;//Date, Open, High, Low, Close, Volume,  Adjusted         Close
}

Stocks::Stocks()
{
//dtor
}
void Stocks::addDay(ticker data){
annual[index].date=data.date;
annual[index].open=data.open;
annual[index].high=data.high;
annual[index].low=data.low;
annual[index].close=data.close;
annual[index].volume=data.volume;
annual[index].adjustedclose=data.adjustedclose;
index--;//most recent data is inserted at bottom (Chronologically)
}
void Stocks::maxprofit(){
int index=0;
int x=1;
while(index<254){
if(annual[index].open<annual[index].close){
    shares=money/annual[index].open;
    money+=shares*(annual[index].close-annual[index].open);
    while(x<254){//when you decide to sell
    if(annual[index].close>annual[index+x].open){
        index=x;
        x=254;
        }
    else{x++;}
    }
    index++;
    }
    else{index++;}
}
cout<<"Portfolio value using the optimal trading strategy after 1 year: $"<<money<<endl;
}
void Stocks::maxloss(){
int index=0;
int x=1;
while(index<254){
if(annual[index].open>annual[index].close){
    shares=money/annual[index].open;
    money+=shares*(annual[index].close-annual[index].open);
    while(x<254){//when you decide to sell
    if(annual[index].close>annual[index+x].open){
        index=x;
        x=254;
        }
    else{x++;}
        }
    index++;
    }
    else{index++;}
}
 cout<<"Portfolio value using the worst trading strategy after 1 year: $"<<money<<endl;
}
void Stocks::oneyear(){
money=1000;
shares=money/annual[0].open;
money+=(shares*annual[0].open-shares*annual[254].close);
//Color(12,"N Hey! I'm in color!")
cout<<"portfolio value after 1 year: $"<<money<<endl;
}
void Stocks::sixmonths(){
money=1000;
shares=money/annual[0].open;
money+=(shares*annual[0].open-shares*annual[127].close);
//Color(12,"N Hey! I'm in color!")
cout<<"portfolio value after 6 months: $"<<money<<endl;
}
double dividends::addDiv(double annualdiv){
money=1000;
money=money+annualdiv;
return money;
}
void dividends::oneyear(){
money=1000;
money+=(shares*annual[0].open-shares*annual[254].close);
shares=money/annual[0].open;
cout<<"Apple portfolio amount including dividends: $"        <<money+annualdiv<<endl;
}

我注意到你在Account课后缺少;。不确定这是否是错误的原因,但我认为这需要修复