0

I am trying to pass an object (of class Stock) by const reference to a function of another class (called Algorithms).

//Algorithms.h
#pragma once

class Algorithms
{
public:
    Algorithms(void);
    ~Algorithms(void);
    int Algorithms::doAnalysis(const Stock&);
};

The implementation of doAnalysis is

#include "StdAfx.h"
#include "Algorithms.h"
#include "Stock.h"
#include <vector>

using namespace std;

Algorithms::Algorithms(void)
{
}

Algorithms::~Algorithms(void)
{
}

int Algorithms::doAnalysis(const Stock &S)
{
    //Do Something
    return 0;
}

The class Stock has the following constructors

public:
    Stock(std::string market, std::string symbol);
    Stock(std::string market, std::string symbol, std::string start_date, std::string  end_date);

I am getting the following error:

Error: declaration is imcompatible with "int Algorithms::doAnalysis(const<error-type> &)" declared at line 8 of  Algorithms.h

I understand that the class Stock is not being found. How should I declare the doAnalysis method in Algorithms.h so that it is found? Stock is not a derived class.

Thanks for your help. I am new to C++.

  • This isn't the actual problem but, don't repeating the class name in the header declaration. in Algorithms.h change int Algorithms::doAnalysis(... to simply int doAnalysis(... – Nathan Monteleone Jan 29 '14 at 14:35
  • In your header, add a forward declaration of `Stocks`. And remove `Algorithms::`. – Jarod42 Jan 29 '14 at 14:35
  • I think there may be a typo. Try changing Algorithms::doAnalysis(const Stock &S) to Algorithms::doAnalysis(const Stock& S) – xspydr Jan 29 '14 at 14:38
  • @luk32: the `#include "Stock.h"` is in `.cpp`, the declaration of `Stock` is missing in `Algorithms.h`. A forward declaration suffice in this case. – Jarod42 Jan 29 '14 at 14:44
  • @Jarod42 Yea, I figured it out and deleted my question. I am used to include all the dependencies in the header file. The way it is organized now is bizarre to me, and got me confused. – luk32 Jan 29 '14 at 14:50

3 Answers3

7

You have to add a forward declaration of the class Stock:

// Forward declaration
class Stock;

class Algorithms
{
public:
    Algorithms(void);
    ~Algorithms(void);
    int doAnalysis(const Stock&);
  //    ^^ <- Remove the Algorithms::
};

You can see here why a forward declaration is necessary in C++.

Community
  • 1
  • 1
Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62
  • Hmmm... don't you think that instead of forward declaration it would be better to move the inclusion of the appropriate headers to the `"Algorithms.h"`? It's just an opinion but I wonder what is others' take at it. – luk32 Jan 29 '14 at 14:42
  • 1
    it's good practice to use forward declaration whenever possible. As programs get larger the number of headers you have to include increases. It's nice to only include a header when you really have to and reduce compile time. – Boumbles Jan 29 '14 at 14:45
  • Thanks. it worked. Just a followup question. Why should I remove Algorithms::? – user2877289 Jan 29 '14 at 14:46
  • @user2877289 When you declare a member function within the class you do not need to use the [Scope resolution operator](http://www.tutorialspoint.com/cplusplus/cpp_class_member_functions.htm), you will need it when you want to define the member function outside the class. – Pierre Fourgeaud Jan 30 '14 at 10:59
  • 1
    @luk32 Like Boumbles said, it is a good practice to use forward whenever it is possible. **[This post](http://stackoverflow.com/a/9906429/1394283)** has a pretty good explanation of why using forward declaration is better. – Pierre Fourgeaud Jan 30 '14 at 11:02
  • @PierreFourgeaud Thank you good man, it's a good read. Now I need to rewrite my codes =( – luk32 Jan 30 '14 at 13:14
1

Put a forward declaration outside your class declaration:

class Stock;

class Algorithms
{
   // ...
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
0

You could also just add #include "Stock.h" in Algorithms.h file and remove the include from the cpp file. Also you don't need Algorithms:: in the declaration of doAnalysis in Algorithms.h

theAlias
  • 396
  • 1
  • 14