1

i have this error in the title: here class declaration of variables and prototypes of function

#ifndef ROZKLADLICZBY_H
#define ROZKLADLICZBY_H


class RozkladLiczby{
public:
    RozkladLiczby(int);                  //konstruktor
    vector<int> CzynnikiPierwsze(int); //metoda
    ~RozkladLiczby();
};  


#endif

And class body:

#include "RozkladLiczby.h"
using namespace std;
#include <iostream>
#include <vector>




RozkladLiczby::~RozkladLiczby()         //destruktor
{}

RozkladLiczby::RozkladLiczby(int n){
int* tab = new int[n+1];
int i,j;

for( i=0;i<=n;i++)
    tab[i]=0;                  //zerujemy tablice

for( i=2;i<=n;i+=2)
    tab[i]=2;                  //zajmujemy sie liczbami parzystymi

for(i=3; i<=n;i+=2)
    for(j=i;j<=n;j+=i)         //sito erastotesa
        if(tab[j]==0)
            tab[j]=i;

}

   vector<int> RozkladLiczby::CzynnikiPierwsze(int m){
vector<int> tablica;
while(m!=1){
        tablica.push_back(tab[m]);
        m=m/tab[m];
}

return tablica;

}

Whats wrong with the prototype of function in first block? Why vector is told to be not a type? I would be grateful if u could help me to find out this.

user3402584
  • 410
  • 4
  • 8
  • 18

2 Answers2

3

Your header file does not include the vector header. Add a #include <vector> to the beggining.

Besides, you should refer to it as std::vector<int> instead of vector<int>, since it belongs to the std namespace. Declaring using namespace x; in header files is not a good practice, as it will propagate to other files as well.

Shoe
  • 74,840
  • 36
  • 166
  • 272
mateusazis
  • 43
  • 4
2

Change your header file to:

#ifndef ROZKLADLICZBY_H
#define ROZKLADLICZBY_H
#include <vector>

class RozkladLiczby{
public:
    RozkladLiczby(int);                  //konstruktor
    std::vector<int> CzynnikiPierwsze(int); //metoda
    ~RozkladLiczby();
private:
    int* tab;
};  


#endif
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • okey now its okey but the new problem has arrived.In Rozkladliczby.cpp it says 'tab' was not declared in this scope. – user3402584 Mar 22 '14 at 12:41
  • The compiler is right, it's not declared in scope of `CzynnikiPierwsze()`. BTW: Ask one question after another in SO please. If my answer fixed the problem stated n your question, consider accepting it. – πάντα ῥεῖ Mar 22 '14 at 12:44
  • I think i accepted it now. Still dont know why its not in CzynnikiPierwsze(). THis method was in public: block so when constructor create an object a tab was created. And this method still has acces to it. SO where should i put this declaration. – user3402584 Mar 22 '14 at 12:50
  • Did you mean to make `tab` a member variable of your class instead of a local definition in your constructor function? – πάντα ῥεῖ Mar 22 '14 at 12:51
  • I want to make tab-member of constructor to be provided to function CZynnikiPierwsze() of the same class. – user3402584 Mar 22 '14 at 12:55
  • @user3402584 Take a look at my edit, how to declare a class member variable properly. – πάντα ῥεῖ Mar 22 '14 at 12:58
  • THanks,there is now error now. Now i have an error when trying to build this files in main program i get that there is undefined reference to my class functions,constructors. Any ideas whats wrong? – user3402584 Mar 22 '14 at 13:05
  • As mentioned, ask a new question about different problems. But you should first take a look at [this post](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) for solving your particular ones. – πάντα ῥεῖ Mar 22 '14 at 13:08