C++ :: Returning A Different Data Type Depending On A Variable?

May 2, 2013

Okay, I've been working on this Texture class that's going to be compatible with both SDL and OpenGL with the capability to create an array of textures within one class as opposed to multiple classes and such.

Now I've run into a slight issue if I want to return the value of a texture. There's two different types of textures: SDL_Texture, and a standard GLuint Texture. The problem I have is that one or the other is used, not both which is depending on whether or not the person is using OpenGL.

So when the user wants to get the texture, I need the ability to return either an SDL_Texture, or GLuint depending on whether or not OpenGL is being used.

I tried this with a template class, but it didn't work, but I'll post the code so you can see what I'm trying to do.

template <class Tex> Tex Texture::GetTexture(int TextureNumber)
{
if (TextureNumber > NumTextures || TextureNumber < 0)
return;

[Code]....

It basically just comes down to the last four lines of code. If the person is Using OpenGL return a GLuint, if the person is using SDL, return an SDL_Texture.

I would prefer to have the GetTexture function to be one function instead of two separate ones so I don't have to call a different function every time to check if I'm using SDL or OpenGL.

View 4 Replies


ADVERTISEMENT

C++ :: Char Data Type Variable

Jun 12, 2013

I have two char variables, m_GPSOffset[13] and m_FileName[100]. When m_GPSOffset has a value assigned to it, say for instance +11:25:30. The first entry of the value, in this case +, is always stored in m_FileName. I am clueless on why this is occurring.

View 15 Replies View Related

C++ :: Change Behaviour Of Template Class Depending On Type?

Jul 8, 2012

is there a way to have a template class respond to missing stuff in a template type ?

Code:
template <typename Type>
class MyClass
{
public:
enum { ID = Type::ID }; // revert to 1 if Type::ID doesn't exist.
};

If the Type passed to the template has an ID member (required to be an enum or a static const int), use it, if it is missing revert to a default value.

I can use this as a simplified way of configuring how MyClass works, without requiring Type to explicitely needing to define what it doesn't care about.

It needs to be resolved at compiletime, as it determines the number of elements in member array variables.

View 10 Replies View Related

C++ :: Convert String Variable For The Year To Integer Data Type

May 12, 2014

My intent was to convert the string variable for the year to an integer data type. The code compiles but now cannot run on my system. I'm not sure what's going as to what the program is displaying.

Objective: Prompt the user for two years. Print all Comedy movies that were released between those two years.

#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <cctype>
using namespace std;
struct Movie {
string name;

[Code] .....

View 2 Replies View Related

C/C++ :: Change Enum Type Variable To String Type Variable?

Aug 10, 2014

How to change an enum type variable to a string type variable?

View 2 Replies View Related

C++ :: Returning A Structure As Function Return Type

Apr 4, 2013

I am having problems with my function definition of a function that should return a structure value.

This is the error I get compute.cpp(9): error C2146: syntax error : missing ';' before identifier 's_advertisebus'

The error is on the line where I start my function definition typing my function type as a structure. A long time ago in c the keyword struct is used with the structure type like struct s_advertisebus s_readadbus(). I tried it both ways but I got errors.

// struct.h
#ifndef STRUCT_H
#define STRUCT_H

struct s_advertisebus {
int nnumberofads;
float fpercentused;

[Code] ....

View 2 Replies View Related

C++ :: Returning Variables As Char Type With If Statement

Feb 17, 2014

Ive been getting an odd error with this code when I try to compile it, as well as Im not quite sure as how to return my variable "compType" as a char type.

Main
#include <iostream>
#include "Shape.h"
#include <iomanip>
#include <cmath>

using namespace std;
void inputShape( char shape)

[Code] ....

View 1 Replies View Related

C++ :: Type Conversion - Float Or Double Variable Into Unsigned Char Variable And Back

May 10, 2013

I would like to convert a float or double variable into unsigned char variable and back.

float number_float = 23.453f;
unsigned char* number_char = (unsigned char*)malloc(sizeof(float));
number_char = reinterpret_cast<unsigned char*> (&number_float);
float* number_float_0 = reinterpret_cast<float*>(&number_char);

I am not getting the same value back.. why?

View 2 Replies View Related

C :: Returning A Pointer To Local Variable

Aug 18, 2014

Should i never return a pointer to a local variable. i have seen the following code in the standard text book.

Code:

struct node *add_to_list(struct node *list, int n)
{
struct node *new_node;
new_node = malloc(sizeof(struct node));
// some code here
return new_node;
}

new_node is a local pointer then why is he returning the new_node? Is it wrong or am i failing to understand something?

View 2 Replies View Related

C++ :: Returning Address Of A Local Variable?

Jul 9, 2014

I know that this code is wrong because it returns the address of local variable which no longer exist after function:

int *f1()
{
int x = 2;
return &x;
}

However, why this one does not show problem and runs fine, here the pointer p is also a local variable:

int *f2()
{
int x = 2;
int *p;
p = &x;
return p;
}

View 6 Replies View Related

C/C++ :: Quicksort - Returning Appropriate Index Variable

Feb 17, 2015

int sift(int a[], int b[], int n, int p) {
int i,k,x, tmp,index=0,count=0;
for(x=0; x<n; ++x)
b[x]=a[x];

[Code] .....

This is a function for a quicksort... Everything works; however, when I return index it returns the value for 'n'... I printed the value for index right before the function returns it and its 4, as it should be.

View 1 Replies View Related

C++ :: Wrong Return Type When Returning Char Array As Pointer

Apr 25, 2014

I am writing a class Player which has several char arrays as private fields. I am trying to write a method which returns an array as a pointer, but doesn't alter the array in any way, thus the const.

Here is a snippet:

Code: class Player
{
private:
char state[MAX_STATE_CHAR + ONE_VALUE];
int rating;
char last[MAX_NAME_CHAR + ONE_VALUE];
char first[MAX_NAME_CHAR + ONE_VALUE];
int groupNumber = NEG_ONE;
public:
char * GetFirst() const
{
return first;
}

Visual studio is saying that the return type doesn't match.

View 3 Replies View Related

C++ :: Function Not Returning Value (Int Data)

Sep 27, 2013

I have a function, which has to return some int data. In that function there are multiple return statements. There is also possibility that function may return in some cases. Will this result in undefined behavior???

View 3 Replies View Related

C++ :: Returning A Pointer To Encapsulated Data Member

Jun 22, 2014

So, I've got this class in SDL Player that has, among other things, an SDL_Texture* to hold an image that represents the player on the screen. I'd assume it's good practice to do get() and set() functions for the class; but because textures are handled via pointers, when I write a get() function I end up returning a pointer to an internal resource; which isn't good practice I hear as it "breaks" encapsulation.

Find my code below:

#ifndef PLAYER_H
#define PLAYER_H
#include "SDL.h"
#include "SDL_image.h"
#include "CTexture.h"
class Player {

[Code] .....

View 1 Replies View Related

C :: Create New Data Type Data Declared As Int

Feb 1, 2015

I'm trying to create a new data type Data declared as int. What comes up to my mind is to create a structure like

Code:

typedef struct Data {
int number;
} Data;

but when I need to use the data I need to go through the structure. Is this a good way to declare a new data type?

View 2 Replies View Related

C++ :: Variable Xxx Is Not A Type Name

Feb 8, 2015

I'm attempting to pass a couple of variables over to my Item.cpp class, that is description and item_price. However under item.set(description,item_price), i get three errors. One under the . (period) saying : expected an identifier and two more under description and item_price stating that variable " xxx " is not a type name.

Main.cpp

#include <iostream>
#include "item.h"
using namespace std;
using namespace items;
int main(){
int n;

[Code] .....

View 11 Replies View Related

C :: Recast Variable X Into Another Type

Jul 24, 2014

I have code already and am looking to incorporate something new into it. I am trying to specify a bit size. A snippet of something similar I have is,

Code:
//header.h
int x; Code: //main.cpp
int func1(int a) {
x = a;
}

Is there anyway I can recast the variable x into another type? For example, if func2 had a char parameter instead, can I somehow make x become char type?

View 8 Replies View Related

C++ :: Proper Way To Define A Variable Type

Sep 17, 2013

I just compiled some code I've been working on at a different OS/compiler and realised that Code: sizeof(unsigned long) returns 4 in one pc and 8 in another.

I've heard that bytesize conventions for basic variables were not particularly "universal" before but this is the 1st time I've had a problem with it.

how do I make a typedef that clearly indicates to whatever compiler compiler I want u32 to be an 32bits unsigned and u64 to be 64bits?

View 12 Replies View Related

C++ :: Counting Number Of Each Type Of Variable

Apr 23, 2013

I'm a basic C++ programmer, and I am stuck on this problem. You work for a company that collects a set of numbers. The numbers are located in a data file named "./Data_File". The data file contains two columns. how do you count a certain number on the left column.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;
#define DATA_FILE1 "./Data_File"
#define SUCEESS 0
#define FAIL 1

[code].....

View 1 Replies View Related

C/C++ :: Variable Has Incomplete Type Matrix

Apr 18, 2015

The code below is supposed to be a program that allows the user to enter in 2 matrices and add them together, and gives an error when they're not the same dimensions.

The error I'm getting is on line 11 of MAIN.CPP and is saying "The Variable has incomplete type 'Matrix'".

MAIN.CPP

#include <iostream>
#include <vector>
#include <string>
#include "Matrix Class Input.h"
using namespace std;
int main() {
Matrix A,B,C;
int r,c=0;

[Code] .....

View 5 Replies View Related

C/C++ :: Finding Type Of Variable Given In Program?

Jul 25, 2012

for ex: say i'm declaring two variables under int type and some 3 under char,my output should be lyk this: 2 variables in int and 3 var of type char...(input to the main program is actually another program where these 2 int and 3 char are defined).

View 1 Replies View Related

C++ :: Assigning Reference Variable To Another Of Same Type

Jun 4, 2012

What is wrong with assigning a reference variable to another reference variable of the same type? I guess I have not understood references very well.

1- In below code, the initialization list gets error because agent "object reference variable" cannot be initialized with a reference variable of the same type.

Code:
class Intention {
public:
Intention(Agent& agent,int Id, string name);

[Code] ....

In other places I have the same problem. In below code the assignment gets error (no overloaded function for assigning a reference to another reference?)

Code:
void Agent::setWorld(World& world) {
this->world = world;
}

//Note: this->world has a type of World&

2- In this other one, I want to assign reference of a vector member to a reference variable of the same type.

Code:
vector<Intention> intentions;
...
Intention& currentIntention = intentions[intentionsIterator]

View 6 Replies View Related

C++ :: Create 1 Variable That Accept Any Type Of Values?

Aug 24, 2013

Can I create 1 variable that accept any type? And can I give it the NULL value too?

View 14 Replies View Related

C++ :: Any Variable Type That Can Change Its Value In Global Scope

Nov 17, 2013

is there any variable type(or with another keyword) that we can change it's value in global scope?

View 5 Replies View Related

C++ :: Initializing Static Map Of Variable Type Abstract Class?

Dec 3, 2014

A have two classes, one inheriting the other, and the parent class being abstract (I plan on adding more child classes in the future). For reasons I won't bother mentioning, I'm making use of an STL container as a way for me to access all of the child objects in the heap. I've done so by making use of a map, with key type int and value type being a pointer to the parent class:

//PARENT.H
class Parent {
protected:
static int n;
static std::map<int, Parent*> map;
public:
virtual void pureVirtual() = 0;

[code]....

The Problem:In line 5 of Parent.cpp, initializing the value of the element to new Child won't work, as according to the compiler, the Child class hasn't been declared yet, and including Child.h into the Parent.h only opens an even bigger can of worms.I also can't initialize it as new Parent, seeing as the parent class is an abstract one.

The Question:Is there a way I can initialize the static map properly. Making the Parent class abstract is not an option.

View 3 Replies View Related

Visual C++ :: Pointer To Function Type - Calculating New Variable?

Nov 22, 2012

I'm fairly new to C++ and have begun working with pointers. I wish to create am array called sigmaf_point that reads data from a text file. I have managed to get that working, but when it comes to using this pointer I come across some problems. The array is created as such:

Code:
double sigma [5];
double *sigmaf_point = sigma;
void read(double *&sigmaf_point)
string s;
ifstream Dfile;
std::stringstream out;

[Code] .....

I then create a coordinate system inside the main file, as the program I am writing is about modelling the movement of atoms, which requires you to know the coordinates:

Code:
int main();
double **coords_fluid = new double*[5000];
for (int i = 0; i < n_atoms_methane; i++) {
coords_fluid[i] = new double[4];
}

Now, the problem arises when I want to calculate a new variable as so:

Code:
for (int i = 0; i <= n_atoms-1; i++) {
sf1=sigmaf_point(coords_fluid[i][3]);
}

I get the error C2064: term does not evaluate to a function taking 1 arguments, and a red line under sigmaf_point that says it must be pointer to function type. I am a bit confused about this.

View 3 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved