Just when I think I understand this stuff some else crops up to bite me. This part was supposed to be the easy part of the overall project so this does not bode well... What am trying to do is create an array of 32 variables that will be filled later on by another class. So all I need this class to do is initialize an array and allow me to return its values. Should be easy peasey. However; I am clearly overlooking something simple.

class Telegraph{
public:
	Telegraph(){
		int const size = 32;
		int *telegraph = new int[size];
	}
	
	~Telegraph(){
		delete[] telegraph;
	}

	int *telegraphValues(){
		for(int i = 0; i < 32; i++){
			return telegraph[i];
		}
	}

private:
	int *telegraph[];
};
#include <iostream>
#include "Telegraph.h"
using namespace std;

int main(){

	Telegraph *one = new Telegraph();
	for(int i = 0; i < 32; i++){
		one[i] = i;
                i++;
	}
	cout << one->telegraphValues;
}

and these are my errors:

------ Build started: Project: TelegraphTest, Configuration: Debug Win32 ------
  TelegraphTest.cpp
c:\documents and settings\dee\my documents\visual studio 2010\projects\telegraphtest\telegraphtest\telegraph.h(19): warning C4200: nonstandard extension used : zero-sized array in struct/union
          Cannot generate copy-ctor or copy-assignment operator when UDT contains a zero-sized array
c:\documents and settings\dee\my documents\visual studio 2010\projects\telegraphtest\telegraphtest\telegraph.h(9): warning C4154: deletion of an array expression; conversion to pointer supplied
c:\documents and settings\dee\my documents\visual studio 2010\projects\telegraphtest\telegraphtest\telegraphtest.cpp(9): error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
          c:\documents and settings\dee\my documents\visual studio 2010\projects\telegraphtest\telegraphtest\telegraph.h(20): could be 'Telegraph &Telegraph::operator =(const Telegraph &)'
          while trying to match the argument list '(Telegraph, int)'
c:\documents and settings\dee\my documents\visual studio 2010\projects\telegraphtest\telegraphtest\telegraphtest.cpp(12): error C3867: 'Telegraph::telegraphValues': function call missing argument list; use '&Telegraph::telegraphValues' to create a pointer to member
c:\documents and settings\dee\my documents\visual studio 2010\projects\telegraphtest\telegraphtest\telegraphtest.cpp(12): error C3867: 'Telegraph::telegraphValues': function call missing argument list; use '&Telegraph::telegraphValues' to create a pointer to member
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Recommended Answers

All 4 Replies

Okay take two. I managed to work through to brain fart of that previous attempt and now I have something compiles. Now my issue is returning an array. It seems to be a common problem but I can't find a solution that fits what I need.

class Telegraph{
public:
	Telegraph(){
		int const size = 32;
		int *telegraph = new int[size];
	}
	
	~Telegraph(){
		delete[] telegraph;
	}

	void setTelegraphValues(int *value, int arrayIndex){
		telegraph[arrayIndex] = value;
	}

	int *getTelegraphValues(){
		for(int i = 0; i < 32; i++){
			return telegraph[i];
		}
	}

private:
	int *telegraph[];
};
#include <iostream>
#include "Telegraph.h"
using namespace std;

int main(){
	Telegraph *tele = new Telegraph();
	for(int i = 0; i < 32; i++){
		tele->setTelegraphValues(&i, i);
		i++;
	}
	cout << tele->getTelegraphValues();
	system("PAUSE");
}

The main is simply for testing to make sure the set and get works before starting on the classes that will actually fill the arrays that will be created. However; my cout statement returns an address value rather than the values of the variables in the array. I could have the get statement simply use cout, that would work but I don't believe the values in the array are going to be printed to a screen in the final product so I don't want to assume the output type I just want to return the values in the array for some manipulation.

I don't understand how your code does compile, it shouldn't. Here are some comments and errors:

class Telegraph{
public:
  Telegraph(){
    int const size = 32; //make this value a static const data member.
    int *telegraph = new int[size]; //telegraph is a static array, this should not be allowed.
  }
	
  ~Telegraph(){
    delete[] telegraph;
  }

  void setTelegraphValues(int *value, int arrayIndex){
    telegraph[arrayIndex] = value;
  }

  int *getTelegraphValues(){
    for(int i = 0; i < 32; i++){
      return telegraph[i];       //this will return only the first element of telegraph, and will stop there.
    }
  }

private:
  //Here, you declare a static array of pointers to integers. The size of the 
  // array should be determined from the initialization list {, , ,} following it
  // but you have no such list (and can't have one for a non-static data member), 
  // and thus, the compiler should not allow this.
  int *telegraph[]; 
};

Now, I am not sure of your intent. My best bet is that you are trying to store an array of pointers to integers. In that case, here is a more suitable implementation using a static array:

class Telegraph{
public:
  static const int size = 32;

  Telegraph() { }

  void setTelegraphValues(int *value, int arrayIndex) {
    telegraph[arrayIndex] = value;
  }

  int *getTelegraphValues(int i) const {
    return telegraph[i];
  }

private:
  int *telegraph[size]; 
};

However, the main function also has major problems. You are storing pointers to integers in your array. And your main function is setting all the array elements to point to the same integer (the index i ). They will all store the same pointer, and as soon as you exit the loop, that pointer will become invalid (because i disappears, i.e., goes out of scope).

You have to clarify whether you intend to store integers or pointers to integers.

Also, if you just need a class that holds an array of integers or pointers to integers and allow you to set/get values at will, I would recommend you just use an STL container which are made for that very purpose, that is, std::vector<int> or std::vector<int*> for example, see here.

Thanks for the response. All I really need this to do it take values past by a user and store them in an array that it later sent to something else and possible printed to the screen where ever its sent. From what I understand user inputed data is converted to binary and stored in the array which is then sent over serial to something else to be used or displayed. All I need is the ability to set values to the array and get them back out. It should be simple but clearly I'm missing some fundemental aspects of C++ here.

ETA: I just read this part

"Also, if you just need a class that holds an array of integers or pointers to integers and allow you to set/get values at will, I would recommend you just use an STL container which are made for that very purpose, that is, std::vector<int> or std::vector<int*> for example, see here."

which is indeed what I need I think...

Here is a simple example using std::vector:

#include <iostream>
#include <vector>
using namespace std;

int main(){
	vector<int> tele(32); //initialize to hold 32 integers.
	for(int i = 0; i < 32; ++i) {
		tele[i] = i;
	}
        for(int i = 0 ;i < 32; ++i) {
  	         cout << tele[i];
        }
	system("PAUSE");
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.