0

I have this code

enum type {NOTHING, SOMETHING, SOMETHINGELSE}
type *x;

At the moment I use x[765] == SOMETHING for example, How would I store other values for example

x[765] == SOMETHINGELSE;
x[765].position == 43.5;
x[765].somevar == 12;

I will apologize for my poor wording within my question im just starting out in C++, I know what I want i'm just not to sure on how to ask it.

Thanks.

3
  • 1
    == is the comparison operator. If you want the assignment operator then that is a single =. Commented Oct 27, 2011 at 20:13
  • 2
    If you're just starting out may I suggest one of the fine introductory books on the book list? stackoverflow.com/questions/388242/… Commented Oct 27, 2011 at 20:14
  • I realize this, its just for demo purposes to show an example value :) Commented Oct 27, 2011 at 20:15

5 Answers 5

2

It looks as if you're looking for a way to structure 'knowledge'; this is done with a struct or a class:

#include <vector>

struct Info {
   enum thingness { nothing, something };

   // 'member' variables
   thingness howMuch;
   int a_counter;
   float position;
};

int main(){
  Info object;
  object.howMuch=Info::something;
  object.a_counter=1;
  object.position=5.4;

You can group these kinds of objects into a container - typically an std::vector:

  // a container of InterestingValues
  std::vector<Info> container(300);

  container[299].howMuch=Info::nothing;
  container[299].a_counter=4;
  container[299].position = 3.3;

  // or assign rightaway:
  container[2] = object;
}
Sign up to request clarification or add additional context in comments.

1 Comment

My compiler is returning: error: object does not name a type
1

You will have to make yourself a more complex type:

struct type
{
    enum flag_type
    {
        NOTHING, SOMETHING, SOMETHINGELSE
    } flag;
    double position;
    int somevar;
};

and later have an array of this new type.

Comments

1

Get yourself a good book to learn from. A list of good books is available here: The Definitive C++ Book Guide and List

In C++, you are asking how to declare an array of structures. Try this:

struct type {
    double position;
    int somevar;
};

type *x;
x[765].position = 43.5;
x[765].somevar = 12;

Comments

0

An enum is a replaceable label basically for an int. You need to define a struct or a class.

struct type
{
   float position ;

};

type var;
var.position = 3.4;

Comments

0

Your type enum would need to be a member of a class, along with the other fields. For example,

class MyType
{
public:
    type t;
    double position;
    int somevar;
};

With an array of MyType instances

MyType *x;

you would then be able to do what you ask expect you would need to do

x[765].t = SOMETHINGELSE;

to assign to the enum.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.