I'm building a data structures project using cmake and I built an array class.
I used std::size_t as a parameter for my default constructor.
But when I try to build the project an error appears saying Invalid use of ::
I tried using namespace std; but also didn't work.
barra.h file
#ifndef BARRAY_H
#define BARRAY_H
class BArray
{
public:
BArray() = delete; //Declare the default constructor as deleted to avoid
//declaring an array without specifying its size.
BArray(std::size_t);
BArray(int, int); //Constructor that initializes the array with init_val.
private:
int* array;
int length;
};
#endif // BARRAY_H
And barray.cpp
#include <iostream>
#include "barray.h"
BArray::BArray(std::size_t init_size)
{
array = new int[init_size]();
length = init_size;
}
BArray::BArray(int init_size, int init_val)
{
array = new int[init_size];
length = init_size;
for(int i = 0; i < init_size; ++i)
array[i] = init_val;
}
Error message:
error: invalid use of ::
Add the following header file and it should work.
#include <cstddef>
Related
I have an 'IntList' class with a dynamic array of integers, but the following fragment of test code gives me troubles:
main.cpp
#include <iostream>
#include "IntList.hpp"
using std::cout;
using std::endl;
int main(int argc, const char * argv[]) {
IntList list{};
cout << "list-1 -> " << list << endl;
return 0;
}
IntList.hpp:
#ifndef IntList_hpp
#define IntList_hpp
#include <stdio.h>
using std::ostream;
class IntList
{
public:
int *dynarray;
int capacity;
int used;
IntList();
void pushBack(int x);
int getCapacity();
void print(ostream& sout);
};
#endif
IntList.cpp
#include <iostream>
#include "IntList.hpp"
using std::cout;
using std::endl;
using std::string;
using std::ostream;
IntList::IntList()
{
int capacity = 1;
int used = 0;
int *dynarray = new int[capacity];
}
ostream& operator<<(ostream& sout, const IntList& list)
{
for (int i = 0; i < list.used; ++i)
sout << list.dynarray[i] << " ";
return sout;
}
From what I understood, I tried to overload the << operator with this:
invalid operands to binary expression ('ostream' (aka 'basic_ostream<char>') and 'ostream')
but I don't know where I get it wrong because XCode gives me this error:
Invalid operands to binary expression ('basic_ostream<char>' and 'IntList')
Any idea how to solve this ?
It appears (from the fragments you have shown) that there is no declaration of your << override in the header file (IntList.hpp). Thus, the code in your main function is not (and cannot be) aware of that override, which is provided in a separate source file.
You need to add a declaration of that override function in the header (tyically, just after the class definition), like this:
// Declaration (prototype) of the function for which the DEFINITION is provided elsewhere
extern ostream& operator<<(ostream& sout, const IntList& list);
Further, your IntList constructor has some serious faults. In it, you are assigning values to three local variables (whose data will be completely lost when the constructor finishes). Those variables are hiding the member variables with the same names. Use this, instead (i.e. remove the int declaration specifiers):
IntList::IntList()
{
// int capacity = 1; // This declares a local variable that hides the member!
capacity = 1;
used = 0;
dynarray = new int[capacity];
}
I am trying to create an array in my UnsortedList class. I specified to create an array in the header file, and I also specified the MAX_SIZE, which is equal to 10. However, whenever I create my object of the class, the default constructor does not create that array with the MAX_SIZE. I am unsure what I am doing wrong. I also get an error saying "stack around the variable 'myList' was corrupted". Also, just as a side note, can I initialize the array values when the default constructor is called, instead of creating a function to do it?
"UnsortedList.h" header file:
#pragma once
class UnsortedList {
public:
UnsortedList();
bool IsFull(); //Determines whether the list is full or not (returns T or F)
int GetLength(); //Gets the length of the list
void SetListValues();
private:
int length;
const int MAX_ITEMS = 10;
int numbers[];
};
"UnsortedList.cpp" file:
#pragma once
#include "UnsortedList.h"
#include <fstream>
#include <iostream>
using namespace std;
UnsortedList::UnsortedList() {
length = 0; //sets length to 0
numbers[MAX_ITEMS]; //sets array maximum size to MAX_ITEMS (10 as indicated in UnsortedList.h)
}
bool UnsortedList::IsFull() {
return (length == MAX_ITEMS);
}
int UnsortedList::GetLength() {
return length;
}
void UnsortedList::SetListValues() {
ifstream inFile;
inFile.open("values.txt");
int x = 0;
while (!inFile.eof()) {
inFile >> numbers[x];
x++;
}
}
"main.cpp" file:
#include <iostream>
#include <string>
#include "UnsortedList.h"
using namespace std;
int main() {
UnsortedList myList;
myList.SetListValues();
return 0;
}
I recommend you use std::array or std::vector, but if you must use C arrays, then your definition in the header needs correcting:
class UnsortedList {
// ...
const static int MAX_ITEMS = 10;
int numbers[MAX_ITEMS];
};
You can remove the corresponding line in the constructor. The file reading method also needs correcting:
void UnsortedList::SetListValues() {
ifstream inFile;
inFile.open("values.txt");
int x = 0;
int read_value;
// x < MAX_ITEMS to avoid out of bounds access
while (x != MAX_ITEMS && inFile >> read_value)
{
numbers[x++] = read_value;
length++; // I assume you also want to increment the length at this point?
}
}
Edit: As noted by #πάνταῥεῖ, there is no good reason to use C style arrays when the standard provides std::array. Not much changes, it is declared as:
std::array<int, MAX_ITEMS> numbers;
You can use operator[] as with the C array. This is preferable as it provides a richer API and can be used like other C++ containers, i.e. with STL algorithms.
The problem is I have a Sort parent class (Sorter) and many children( merge,bubble,quick)
What I m trying to do is to access the function "runOnce" which is a function in parent class.
Main.cpp
int main()
{
fstream Data;
Data.open("Test.txt",ios::in|ios::out);
Sorter<int>* bubble = new BubbleSort<int>;
Sorter<int>* quick = new QuickSort<int> ;
Sorter<int>* merge = new MergeSort<int> ;
vector<int> list;
while(!Data.eof())
{
int temp;
Data >> temp;
list.push_back(temp);
}
Sorter<int>::runOnce(bubble, list);
Data.close();
}
Sorter.cpp
#ifndef SORTER_H
#define SORTER_H
#include <vector>
using namespace std;
template <class T>
class Sorter {
public:
Sorter(){};
virtual void sort(vector<T>&, int, int) = 0;
double runOnce(Sorter<T>&, vector<T>&);
};
#endif // SORTER_H
but I m getting an error in main in line of
Sorter::runOnce(bubble, list);
it says:
no matching function for call to 'sorter::
runOnce(Sorter*&, std:: vector < int , std :: allocator < int > > &)
And I tried to change it to be
bubble->Sorter->runOnce(bubble, list);
and it said
invalid use of Sorter< int >::Sorter
How can I fix this?
The function takes a Sorter&, not a Sorter*&. Dereference the pointer when passing it in, e.g:
Sorter<int>::runOnce(*bubble, list);
I Solved it with making runOnce a function in the main and calling it and it solved everything. Also I used the header files of the parent class in the children because I was using the cpp file.
In header file:
#ifndef Array_h
#define Array_h
#include "stdafx.h"
using namespace std;
template<class T>
class Arrayc
{
private:
int Arraysize;
int length;
T *array;
public:
Arrayc(int size);
~Arrayc();
};
template<class T>
Arrayc<T>::Arrayc(int size)
{
Arraysize = size;
length = 0;
array = new T[Arraysize];
}
#endif
In main source file:
Arrayc<int> *Arrayofintegers;
Arrayc<float> *Arrayoffloat;
// These lines have the error
Arrayofintegers = new Arrayc<int>::Arrayc(10);
Arrayoffloat = new Arrayc<float>::Arrayc(5);
You only need specify the scoped name, Arrayc<T>::Arrayc, when defining the constructor.
To call the constructor, simply use Arrayc<T>(/*args*/).
Of course, you really don't even need new at all, but that's unrelated to the error.
You are not passing any clues to your constructors of what you want T to be at the time that you use them. I'd suggest adding a parameter T to your constructor, even if you only use it as a phony initializer value, so that the compiler can deduce what type Arrayc() will be, e.g., new Arrayc(10, 0) or new Arrayc (10, 0.0f)
I want to declare a pointer in my .h and initialize it in .cpp. For example an int pointer:
My .h class file:
#pragma once
#include <iostream>
#include <stdio.h>
#include <stdint.h>
using namespace std;
class Calc_ToF_low
{
private:
int telo[3];
public:
Calc_ToF_low(void);
~Calc_ToF_low(void);
double * CalcToF(int16_t * señal, int fs);
long double * filter(long double *ganancias, long double *coeficientes, double *señal,int lensignal, int L, int control);
void signal_p1_lowf(void);
void avg_p1_lowf(void);
void time_est();
};
My .cpp class file:
#include "Calc_ToF_low.h"
Calc_ToF_low::Calc_ToF_low(void)
{
telo[3]={0,1,2};
}
How can I do this?
Like this:
Calc_ToF_low::Calc_ToF_low() // note no need to say void in C++
{
telo[0]=0;
telo[1]=1;
telo[2]=2;
}
Or if you can use C++11, something like this might work:
Calc_ToF_low::Calc_ToF_low()
: telo{{0,1,2}} // initializer list, will not work in "old" C++98
{}
You could just assign values to it by typing:
telo[0] = 0;
telo[1] = 1;
telo[2] = 2;
in your .cpp.
It may not be perfect for huge arrays but then you probably should assign the values from a file instead.
If you're initializing large arrays with a constant value for all item, you can use this approach:
Calc_ToF_low::Calc_ToF_low(){
memset(telo, 0, sizeof(telo)); //0 is the constant value
}
If you want to initialize your large array to some set of values that has no patterns:
int defaultTeloData[TELO_NUMITEM] = {2,4,1,5,6,1,7,82,41,6,134,88,1};
Calc_ToF_low::Calc_ToF_low(){
memcpy(telo, defaultTeloData, sizeof(telo));
}
If your array values has pattern, use a loop and some formulas or constructors.
Calc_ToF_low::Calc_ToF_low(){
for(int a = 0; a sizeof(telo)/sizeof(telo[0]); a++){
telo[a] = dataFormulaForTelo(a);
}
}
This is one way you could do it:
#include <array>
class Calc_ToF_low
{
private:
std::array<int, 3> telo;
public:
Calc_ToF_low();
};
Calc_ToF_low::Calc_ToF_low()
: telo()
{
for(unsigned i = 0; i<telo.size(); ++i)
telo[i] = i;
}