Pointing an array in c++? - c++

I want to point an array in c++ , is it possible ?
My main code :
#include "ArrayPointerClass.h"
#include "stdafx.h"
#include <iostream>
#include <string>
int _tmain(int argc, _TCHAR* argv[])
{
float arr[2];
ArrayPointerClass::pointingArray(&arr);
return 0;
}
ArrayPointerClass.h
#pragma once
static class ArrayPointerClass
{
public:
ArrayPointerClass();
~ArrayPointerClass();
static void pointingArray(float* arr[2]);
};
ArrayPointerClass.cpp
#include "stdafx.h"
#include "ArrayPointerClass.h"
ArrayPointerClass::ArrayPointerClass()
{
}
ArrayPointerClass::~ArrayPointerClass()
{
}
void ArrayPointerClass::pointingArray(float* arr[2]){
float newArray[2] = { 2.2f, 2.2f };
*arr = newArray;
}
I've got this error :
Error 3 error C2653: 'ArrayPointerClass' : is not a class or namespace name c:\users\alex\documents\visual studio 2013\projects\pointerarray\pointerarray\pointerarray.cpp 13 1 PointerArray
Error 3 error C3861: 'pointingArray': identifier not found c:\users\alex\documents\visual studio 2013\projects\pointerarray\pointerarray\pointerarray.cpp 13 1 PointerArray
I know in C++ arrays ,arrays without length defined are not allowed . is it the reason ?
Thanks for your support

There is no way to create a static class in c++. static keyword can be applied to objects and functions.
And each array name is a pointer. Therefore subscripts cannot be given in the parameter. It is sufficient to provide the pointer type.
The modified code which works:
#include <iostream>
#include <string>
using namespace std;
class ArrayPointerClass
{
public:
ArrayPointerClass();
~ArrayPointerClass();
static void pointingArray(float* arr);
};
ArrayPointerClass::ArrayPointerClass()
{
}
ArrayPointerClass::~ArrayPointerClass()
{
}
void ArrayPointerClass::pointingArray(float* arr){
float newArray[2] = { 2.2f, 2.2f };
arr = newArray;
}
int main()
{
float arr[2];
ArrayPointerClass obj;
obj.pointingArray(arr);
return 0;
}

*arr = newArray; will not work you can't copy a C array like that .
You could have done memcpy() or std::copy() like;
memcpy( newArray, arr, 2);
std::copy( newArray, newArray+2, arr);

Thanks everybody for your answers (even there were partially working)
I found my self a solution
// PointerArray.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int* test() {
int size_needed = 2;
int* a = new int[size_needed];
a[0] = 0;
a[1] = 0;
return a;
}
int main()
{
//int arr[2] = { 1, 1 };
int* arr = test();
for (int i = 0; i < 2; i++){
cout << arr[i] << std::endl;
}
cin.get();
return 0;
}

Related

Error C2280 : Class::Class(void) : Attempting to reference a deleted function

So, I am working on a project, and I have two files in this project:
main.cpp, matrix.h
The problem is that My code seemed to work perfectly a few hours ago, and now it doesn't
main.cpp:
#include <iostream>
#include "matrix.h"
#include <vector>
int main() {
Matrix f;
f.create(10, 1, {3, 4, 5, 6, 7, 8, 9});
}
matrix.h:
#pragma once
#include <iostream>
#include <string>
#include <Windows.h>
#include <vector>
class Matrix {
public:
const size_t N;
bool ifMatrixCreated;
const char* NOTENOUGH = "The size of the array should match to the width*height elements";
std::vector<int> arr;
int w, h;
void create(int width, int height, const std::vector<int> a) {
w = width;
h = height;
if (a.size() != width * height) {
ifMatrixCreated = false;
std::cout << "bello";
}
else {
ifMatrixCreated = true;
arr = a;
std::cout << "hello";
}
}
};
And when I compile, it generates this error (Using VS2019):
Error C2280 | 'Matrix::Matrix(void)': attempting to reference a deleted function Matrix | Line 5
It keeps saying that "The default constructor of Matrix cannot be referenced - It is a deleted function"
Can you help solve this error?
Thanks in advance.
Here is the correct working example. The error happens because every const data member must be initialized. And
The implicitly-declared or defaulted default constructor for class T is undefined (until C++11)defined as deleted (since C++11) if any of the following is true:
T has a const member without user-defined default constructor or a brace-or-equal initializer (since C++11).
#pragma once
#include <iostream>
#include <string>
//#include <Windows.h>
#include <vector>
class Matrix {
public:
//const size_t N;//this const data member must be initialised
const size_t N = 6;
bool ifMatrixCreated;
const char* NOTENOUGH = "The size of the array should match to the width*height elements";
std::vector<int> arr;
int w, h;
void create(int width, int height, const std::vector<int> a) {
w = width;
h = height;
if (a.size() != width * height) {
ifMatrixCreated = false;
std::cout << "bello";
}
else {
ifMatrixCreated = true;
arr = a;
std::cout << "hello";
}
}
};

How to access a member of an object in a stack? C++

How to assign/read a member of an object in a stack?
struct item{
char opra;
int count;
double operand;
};
stack<item> S;
double test = S.top.operand;
it not works, thanks.
Top is a method, so you should call S.top().operand. I got it to compile like that:
#include <iostream>
#include <stack>
struct item{
char opra;
int count;
double operand;
};
int main(){
std::stack<item> S;
double test = S.top().operand;
return 0;
}
You are just making a typo.
top in stack is a function, I let you here a working example:
#include <iostream>
#include <stack>
using namespace std;
struct item{
char opra;
int count;
double operand;
};
int main()
{
item a;
a.opra = 'a';
a.count = 3;
a.operand = 5.0;
stack<item> S;
S.push(a);
// Stack top
cout << S.top().operand;
return 0;
}
Output:
5

Incomplete type error when using std::vector with structs

I'm working with c++ STL vectors, and have a vector of structures called projectileList. I'm trying to iterate through the vector, getting and setting values in the struts as I iterate, but my code refuses to compile, with the error 'Incomplete type is not allowed.'
Can anyone please point out what I'm doing wrong:
Code:
ProjectHandeler.h:
#include "stdafx.h"
#include "DataTypes.h"
#include <vector>
class ProjectileHandeler {
private:
int activeObjects;
std::vector<projectile> projectileList;
void projectileUpdater();
public:
ProjectileHandeler(projectile* input[], int projectileCount);
~ProjectileHandeler();
};
#endif
projectileHandeler.cpp
#include "stdafx.h"
#include "DataTypes.h"
#include "ProjectHandeler.h"
#include <vector>
ProjectileHandeler::ProjectileHandeler(projectile* input[], int projectileCount)
{
for (int i = 0; i < projectileCount; i++)
{
projectileList.push_back(*input[i]);
activeObjects += 1;
}
//NO extra slots. Not that expensive.
projectileList.resize(projectileList.size());
}
void ProjectileHandeler::projectileUpdater()
{
while (true)
{
for (unsigned int i = 0; i < projectileList.size(); i++)
{
if (projectileList[i].isEditing == true)
break;
}
}
}
This compiles fine (tested it here: http://codepad.org/cWn6MPJq):
#include <vector>
struct projectile {
bool isEditing;
};
class ProjectileHandeler {
private:
std::vector<projectile> projectileList;
void projectileUpdater()
{
//This bit loops to infinity and beyond! ...or at least untill the handeler is destroyed.
while (true)
{
for (unsigned int i = 0; i < projectileList.size(); i++)
{
if (projectileList[i].isEditing == true) //Throws Incomplete type error
break;
}
}
}
};
int main()
{
}
Notice the removal of *, correct type of loop variable and removal of extra class specifier.

Program stops working when class member function executes

I know there is something wrong with the class member functions because I comment everything in them out and the program will run fine but when I uncomment anything it stops working. The constructor runs fine as well.
Here is my CharArray.h file:
#ifndef CHARARRAY_H
#define CHARARRAY_H
class CharArray
{
private:
char * pArray;
int iSize;
public:
CharArray(int size)
{
char *pArray = nullptr;
iSize = size;
pArray = new char[iSize];
pArray = '\0';
}
void setItem (int loc, char ch);
char getItem (int loc);
~CharArray()
{
delete [] pArray;
}
};
#endif // CHARARRAY_H
Here is my member functions:
#include <iostream>
#include <cstring>
#include <iomanip>
#include <cstdio>
#include "CharArray.h"
using namespace std;
void CharArray::setItem (int loc, char ch)
{
pArray[loc] = ch;
cout << pArray[loc] << endl;
return;
}
char CharArray::getItem (int loc)
{
char c;
c = pArray[loc];
return c;
}
And here is my main file:
#include <iostream>
#include <iomanip>
#include "CharArray.h"
using namespace std;
int main()
{
CharArray myChar (5);
int size;
char cstr[10] = "Drew";
myChar.setItem(1, 'A');
char c = myChar.getItem(5);
cout << c << endl;
return 0;
}
Your first problem is in the constructor:
CharArray(int size)
{
char *pArray = nullptr; // <-- unrelated to the pArray in the object!
iSize = size;
pArray = new char[iSize];
pArray = '\0'; // <-- we just lost the handle to new array
}
That last line should instead be:
*pArray = '\0';
Also, it would be better to use a more modern constructor style such as this:
CharArray(int size)
: pArray(new char[size]),
iSize(size)
{
*pArray = '\0';
}

how to reverse char array using class member pointer?

im tryin to reverse an array using pointer which is a class member:
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
class my_string
{
char* ptr;
int size;
public:
my_string(){};
my_string(char* str) : ptr(str),size(strlen(ptr)){};
char* getstr () {return ptr;};
void reverse();
int find (char);
void print();
};
void my_string::reverse()
{
int size2=size;
for (int i=0;i<(size/2);i++)
{
char tmp=ptr[i];
ptr[i]=ptr[size2-1];
ptr[size2-1]=ptr[i];
size2--;
}
}
int my_string::find(char c)
{
for (int i=0;i<size;i++)
{
if (ptr[i]==c)
return i;
}
return -1;
}
void my_string::print()
{
for (int i=0;i<size;i++)
cout<<ptr[i];
cout<<endl;
}
int main()
{
my_string s1("abcde");
s1.print();
s1.reverse();
s1.print();
}
im not gettin any errors but the reverse function is surely not working.
can someone please explain to me why?
*this is an homework assignment asking me not to use dynamic allocation or strings (for now).
You didn't mention not being able to use standard library algorithms, so
std::reverse(ptr, ptr+size);
You can use standard algorithm std::reverse declared in header <algorithm>.
For example
std::reverse( ptr, ptr + size );
But if you want to do it yourself then the function could look the following way
void my_string::reverse()
{
for ( int i = 0; i < size/2; i++ )
{
char tmp = ptr[i];
ptr[i] = ptr[size-1-i];
ptr[size-1-i] = tmp;
}
}
A test program
#include <iostream>
#include <cstring>
int main()
{
char s[] = "123456789";
char *ptr = s;
int size = std::strlen( ptr );
std::cout << s << std::endl;
for ( int i = 0; i < size/2; i++ )
{
char tmp = ptr[i];
ptr[i] = ptr[size-1-i];
ptr[size-1-i] = tmp;
}
std::cout << s << std::endl;
}
Output is
123456789
987654321