0

I am new to C++, and I am currently working on 2 different projects within the some solution in C++ with VSC and I am encountering the error "A class-qualified name is required"; I have created a custom class in a file inside a namespace like so in project Stock:

//in file Stock.h, project Stock

#pragma once
#include "CandleStick.h"
#include "StockRecord.h"
#include "MarketQue.h"
#include <string>
#include <vector>

namespace Stock {
    class Stock {
    private:
        std::string s_stockID;
        MarketQue* s_topCur;
        StockRecord s_stockRecord;
        std::vector<CandleStick> s_candleSticks;
        unsigned int s_totalVol;
    public:
        Stock(const std::string&, MarketQue&, std::vector<unsigned int>&, unsigned int); //stockID, s_topCurPrice_B, s_topCurPrice_S, s_topCurVol_B, s_topCurVol_S, cs_interval, total_vol
        void updateStock(int);
        ~Stock();
    protected:
    };
}
//in file Stock.cpp, project Stock
#include "Stock.h"

Stock::Stock::Stock(const std::string& stockID, MarketQue& topPrices, std::vector<unsigned int>& csInterval, unsigned int totalVol)
    :s_stockID(stockID), s_topCur(&topPrices), s_stockRecord(StockRecord(topPrices.mq_size)), s_totalVol(totalVol) {
    s_candleSticks.reserve(csInterval.size());
    for (unsigned int i = 0; i < csInterval.size(); i++) {
        s_candleSticks.emplace_back(topPrices.mq_topPrice_S[0], topPrices.mq_topPrice_B[0], csInterval[i]);
    }
};

void Stock::Stock::updateStock(int totalVol) {
    s_totalVol = totalVol;
    s_stockRecord.updateStockRecord(*s_topCur);
    for (unsigned int i = 0; i < s_candleSticks.size(); i++) {
        s_candleSticks[i].updateCandleStick((s_topCur->mq_topPrice_B)[0], (s_topCur->mq_topPrice_S)[0]);
    }
}

Stock::Stock::~Stock() {};

I have successfully linked the 2 projects together (I think, tested it in the main function with a file called App.cpp in project Market):

//in file App.cpp, project Market
#include "Stock.h"
#include "MarketQue.h"
#include <string>
#include <array>
#include <vector>

int main() {
    //initializing
    const std::string stockID = "001";
    Stock::MarketQue topPrices(3);
    std::vector<float> topCurPrice_B = { 3,2,1 };
    std::vector<float> topCurPrice_S = { 4,5,6 };
    topPrices.mq_topPrice_B = topCurPrice_B;
    topPrices.mq_topPrice_S = topCurPrice_S;
    std::vector<unsigned int> topCurVol_B = { 10,11,12 };
    std::vector<unsigned int> topCurVol_S = { 13,14,15 };
    topPrices.mq_topVol_B = topCurVol_B;
    topPrices.mq_topVol_S = topCurVol_S;
    std::vector<unsigned int> cs_Interval = { 100,300,1000 };
    unsigned int totalVol= 3+2+1+4+5+6;
    Stock::Stock first_stock(stockID, topPrices, cs_Interval, totalVol);
    totalVol = 100;
    first_stock.updateStock(totalVol);
    //testing update
    topCurPrice_B = { 13,12,11 };
    topCurPrice_S = { 14,15,16 };
    topCurVol_B = { 110,111,112 };
    topCurVol_S = { 113,114,115 };
    topPrices.mq_topPrice_B = topCurPrice_B;
    topPrices.mq_topPrice_S = topCurPrice_S;
    topPrices.mq_topVol_B = topCurVol_B;
    topPrices.mq_topVol_S = topCurVol_S;
}

However, when I try to use this class(Stock::Stock) as a member inside of another class (in project Market), I received the error mentioned above ("A class qualified name is required");

I first did this:

//in file Market.h, project Market

#pragma once
#include "Stock.h"
#include <string>
#include <vector>

namespace Market {
    class Market {
    private:
        std::string marketID;
        std::vector<Stock::Stock> stock; //received error
        Stock::Stock stock;
    public:
        Market();
        void updateStock();
        ~Market();
    protected:
    };
}

At this point i figured it might be because of the namespace Market making it Market::Stock::Stock and i decided to remove the namespace Market:

//in file Market.h, in project Market

#pragma once
#include "Stock.h"
#include <string>
#include <vector>

class Market {
private:
    std::string marketID;
    std::vector<Stock::Stock> stock;
public:
    Market();
    void updateStock();
    ~Market();
protected:
};

I'd like to ask how to fix this problem, and if possible can someone explain to me what is happenning to the code that caused the error?

Thank you very much for your time.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Tan Saint
  • 62
  • 6
  • `private: int num` -- You are missing a semicolon. Also, don't stick things in your file that are not C++ related if the issue is a compilation error. The reason is that we would like to take your *exact* code, and compile it ourselves. – PaulMcKenzie Jul 20 '20 at 02:59
  • It's possible you might have a header co-dependency here. Hard to know, since you seem to have shown only snippets and none of these files have header guards. Does `N_A.h` include any files? What files? Do your headers have header guards? Show them. – paddy Jul 20 '20 at 03:08
  • yep thank you for the suggestions, i'll clarify the question bit more and show the code; I'd just figured i could try and simplify the question. @PaulMcKenzie – Tan Saint Jul 20 '20 at 03:24
  • yep, sorry for the inconvenience i'll edit the question better. Thanks @paddy – Tan Saint Jul 20 '20 at 03:25
  • 1
    Does `MarketQue.h` include `Market.h`? This looks to be what I suspected. The file `Stock.h` seems to include files that appear to relate to your `Market` class. That means these files will all be included before the `Stock` class is defined. – paddy Jul 20 '20 at 03:38
  • 1
    Please see https://stackoverflow.com/q/3608305/1553090 for a description of essentially the same problem, and an answer that should help. Also see https://stackoverflow.com/a/2059705/1553090 for how to forward-declare a class in a specific namespace. – paddy Jul 20 '20 at 03:41
  • Hey paddy, MarketQue does not include Market.h, I think you are right with my headers being incorrect... I added `#pragma once` on top of **App.cpp**, and it doesn't give me the problem anymore. Thank you very much for the help! – Tan Saint Jul 20 '20 at 03:45
  • Your solution makes no sense. Something is horribly wrong with your project setup. – paddy Jul 20 '20 at 03:52
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/218183/discussion-between-tan-saint-and-paddy). – Tan Saint Jul 20 '20 at 06:16

0 Answers0