C/C++ :: Using Template Function Inside A Class In Separate Function?

Mar 26, 2014

i want to use a class to print data stored as vector or array with different data types.

i also want the print function two take more than one vector or array or combination of both so that they can be written to file as two columns.so i wrote the following class:

right now it has only one member function for printing two vectors. later i'll add additional functions as required.

note: there has to be template functions inside the class / i also want the object to be global so that i need not pass it as an argument to other calling functions

class printdata {
public:
template<typename T1,typename T2>
void SaveData( vector<T1> &data1,vector<T2> &data2, std::string var){
std::ofstream myfile;
std::string filename;

[code].....

then i want to call this template function in another ordinary function written in a seperate cpp file these function declarations are put in a header file. so i need know whether i should put the declaration of the template function in the header to use the function in different functions.

View 1 Replies


ADVERTISEMENT

C++ :: Using Template Function Inside Class In Separate Function?

Mar 26, 2014

i want to use a class to print data stored as vector or array with different data types. i also want the print function two take more than one vector or array or combination of both so that they can be written to file as two columns. so i wrote the following class:

right now it has only one member function for printing two vectors. later i'll add additional functions as required.

note: there has to be template functions inside the class
i also want the object to be global so that i need not pass it as an argument to other calling functions

class printdata
{
public:
template<typename T1,typename T2>
void SaveData( vector<T1> &data1,vector<T2> &data2, std::string var)
{

[Code]....

then i want to call this template function in another ordinary function written in a seperate cpp file

these function declarations are put in a header file. so i need know whether i should put the declaration of the template function in the header to use the function in different functions

View 4 Replies View Related

C++ ::  how To Declare Template Function Inside Template Class

Dec 5, 2013

I'm trying to implement a simple template array class, but when i came into the operator< i actually have to use a template :

my code is something like :

template<typename _Type, std::size_t _Size>
class array {
public :

[Code] ......

but i am having an error of shadows template param 'class _Type' is it w/ the name conflict between the array template parameter and the function template parameter ?

View 6 Replies View Related

C++ :: Separate Template Class Into Two Files

Oct 13, 2014

I have a big big problem with class template

//file Array.h
#ifndef ARRAY_H_
#define ARRAY_H_
template <class T> class Array {

[Code] .....

I've tried many ways:
1. put #include<Array.cpp> at the end of file Array.h
2. Change Array.cpp to Array.hpp,...

But dont work at all

I have tried to separate class template into two files.

View 13 Replies View Related

C/C++ :: Offset Function Inside Class?

Jan 1, 2013

i usually use this method for accesing functions in executables, the code is executed from a DLL (always works, except when the function are inside of a class, even tho is public):

.h:

typedef int (*pgObjViewportClose) (OBJECTSTRUCT* gObj);
extern pgObjViewportClose gObjViewportClose;

.cpp

pgObjViewportClose gObjViewportClose = (pgObjViewportClose) 0x04F1940;

That works, but i can't get it to work if the accesing function is inside of a class, i get Unhandled Exception while trying to access a function inside a class, is there a way to do it?.

View 1 Replies View Related

C++ :: Class Definition Inside Function Body

Mar 3, 2013

Is this standard-compliant code?

int f() {
class C {
public:
int mf() const {return 1;}
};
C c;
return c.mf();
}

View 1 Replies View Related

C++ :: Declare Template Type Object Inside Template Type Class Definition

Oct 12, 2013

Let me put it into the code snippet:

/**
This class build the singleton design pattern.
Here you have full control over construction and deconstruction of the object.
*/
template<class T>
class Singleton

[Code]....

I am getting error at the assertion points when i call to the class as follows:

osgOpenCL::Context *cxt = osgOpenCL::Singleton<osgOpenCL::Context>::getPtr();

I tried commenting assertion statements and then the debugger just exits at the point where getPtr() is called.

View 7 Replies View Related

C++ :: Function In A Class Template

Mar 3, 2013

I have this class templates And This UML.I have to write this function +operator=(source: Array<ElemType, SIZE>): Array<ElemType, SIZE> but I do not know how to start the declaration / or start the function. I have to return a template but I do not know how to do it,

UML
Array<ElemType, SIZE>
-elements: ElemType[SIZE]
+Array()
+Array(source: Array<ElemType, SIZE>)
+operator=(source: Array<ElemType, SIZE>): Array<ElemType, SIZE>
+operator==(other: Array<ElemType, SIZE>): Boolean
+operator!=(other: Array<ElemType, SIZE>): Boolean
<<L-value>>+operator[](index: Integer): ElemType
<<R-value>>+operator[](index: Integer): ElemType

[code]....

View 4 Replies View Related

C++ :: Template Function Of A Class

Feb 3, 2013

I want to use a template function of a class.

This is my code:

#include "Comparison.h"
#include <iostream>
using namespace std;

int main(int argc, char** argv) {
Comparison c;

[Code] ....

But I get the error message:

main.cpp:10: undefined reference to `int Comparison::max<int>(int, int)'

View 2 Replies View Related

C++ :: Template Function As Member Of Class

Apr 15, 2014

I want to have a template function that is a member of a class. Is this possible? This code snippet is how I would think the syntax would go, although it doesn't compile. How would I achieve the same effect?

Code:
class myclass {
public:
int member ;
} ;
template <typename T> void myclass::func( T& arg )

[Code] .....

View 4 Replies View Related

C++ :: Overload Template Function In A Class?

Jun 21, 2013

Firstly, is it legal to overload a template function in a class? This is what I did

class FILE_txt
{
public:
FILE_txt(const char* );

[Code]....

View 19 Replies View Related

C++ :: Argument Deduction Of Function And Class Template

Jun 20, 2013

When we use a function template, we use a function template like a regular function, for example,

Code:
template<class T>
void foo(T t1, T t2)
{
}
foo(1,3);

Based on the arguments passed to foo, the compiler can deduct the type T. But on the other hand, when we use a class template, we always need to specify the type, for example,

Code:
template<class T>
struct sum {
static void foo(T t1, T t2)
{
}
};
sum<int>::foo(1,3);

Here we can't call sum::foo(1,3), otherwise we get compiler errors. My question is why the compiler can't deduct the type based on the arguments passed to foo? In addition, if we call function template foo like this,

Code:
foo(1, '3');

Then we get compiler errors. We need to specify the type like foo<int>(1.'3'). Since '3' can be always treated as integer, why we need to specify the type here?

View 7 Replies View Related

C++ :: Passing A Function Pointer As Template Argument To A Class

Aug 15, 2012

I have in the past written code for templated functions where one function argument can be either a function pointer or a Functor. Works pretty straightforward.

Now I am in a situation where I am actually trying to pass a function pointer as template argument to a class. Unfortunately this does not work, I can pass the Functor class but not the function pointer. Below code illustrates the issue:

Code:
#include <string>
#include <iostream>
#include <sstream>
#include <cstdlib>
// For demonstration
const char * external_library_call() {
return "FFFF";

[Code] .....

The idea is to have the definition of the Record class simple and readable and have a maintainable way to add auto-conversion functions to the class. So the lines I commented out are the desirable way how I want my code to look. Unfortunately I could not come up with any way that was close to readable for solving this.

View 3 Replies View Related

C++ :: Error Returning Template Class From Member Function

Apr 30, 2012

When I do this:

// header file:
#include <list>
using namespace std;
class myClass {
list<int> myMethod();
};

// cpp file:

list<int> myClass::myMethod() {
}

In the cpp file, 'myMethod' is underlined in red and when I hover over it, it says:

"std::list<int, std::allocator<int>> myClass::myMethod()

Error: declaration is incompatible with "<error-type> myClass::myMethod()""

But when I make it as a standalone function, outside a class, with no pre-declaration, there is no problem.

View 8 Replies View Related

C++ :: File IO Inside A Class - No Instance Of Overloaded Function Getline Matches Argument List

Jan 24, 2012

Hey I am trying to use the getline() function to read a line from a file. For some reason Visual Studio 2010 gives me the following error. "No instance of overloaded function "getline" matches the argument list". The piece of code that produces the error is in a class in a separate .h file and is executed as a method of the object. I'm almost certain it has something to do with either the compiler thinking I am calling another getline in a different namespace or my parameters for the function are incorrect. Here is the code:

Code:
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
class InsultGenerator

[Code] .....

View 1 Replies View Related

C/C++ :: Template Class Function Taking A Derived And Generic Object

Feb 23, 2014

I'm trying to write a function for receiving messages, so my classes can communicate with each other. The only issue I get is a compile error asking me to define the base parameter as one of the derived instances. I tried using a void* to fill the need, but then I lose the initial type, which I need to check for. How might I go about writing a generic object for this?

Here's my code:

template<class Object>
class State
{
public:

[Code].....

Should I just have all of the objects inherit in the order of Object >> GenericObject >> DerivedObject?

View 10 Replies View Related

C++ :: Using Template Class - Unable To Match Function Definition To Existing Declaration

May 12, 2013

I am just learning using class template but I keep getting error unable to match function definition to an existing declaration

template <typename T>
class Homework {
private:
string name;
public:
template <typename T>
void SetName(T val);

[Code] ....

View 10 Replies View Related

C++ ::  typedef As Data Type For Specialized Template Function In Class From Shared Library

Jul 19, 2013

I have a class "Result" with a single template function Set(const std::string& arName, T& val) and a specialization of this function Set<Real>(const std::string& arName, Real& val) where Real is a typedef for double. The class is in a shared library and I use it in my main program. If I do result->Set<GLOBAL::Real>("U", 100.0); the wrong template function is called!

I check this by the output with std::cout.

Maybe it's a problem with the typedef.

If I link the object file of the Result class directly to my main program (no shared library), it works.

typedefs.hpp:
namespace GLOBAL {
typedef double Real;
} results.hpp
#include <iostream>

[Code] ....

View 3 Replies View Related

C :: Make A Separate Function

May 6, 2013

I will post the entire code at the bottom. Its running fine right now but This part in the menu function I need to make as a separate function. My problem is when I make it into a function by itself I have to declare and initialize the grades A,B,C,D,F how can I do that when I can't make them equal 0 because it has to keep track of how many of each letter grade.

Code:

//Count letter grade
if(ave >= 90)
++A;
else if (ave >= 80)
++B;
else if (ave >= 70)
++C;
else if (ave >= 60)
++D;
}

[code]....

View 3 Replies View Related

C++ :: Returning Two Separate Variables In A Function?

Dec 12, 2013

How to use a function twice to calculate two different variables. How to have a function compute the city tax, then it uses the function a second time to compute the county tax.

My program compiles, and runs fine, however it only outputs the city tax calculation. It seems it never attempts to calculate the county tax.

One thing: All of the directives I used is what we are limited to, no more, no less. He requires a set format and will dock points of you use anything but those directives and void main().

Here is my code so far:

/* function for city tax calculation */

#include <iostream>
using namespace std;
#include <string>
#include <cstdlib>
#include <cmath>
#include <iomanip>
const double city_tax_rate=0.03;

[Code] ....

I am using Visual studio 2010.

View 9 Replies View Related

C++ :: Template Function Don't Work As Inline Function?

Mar 8, 2014

I have this:

string input;
unsigned short choice;
...
istringstream valid(input);
...
if(!(valid >> choice))
{
//some error
}

Ok. My code is almost 1000 lines, and I have splited some functions in headers. But the same function doesn't work:

template <typename T> bool valid_input(const string& input, T var)
{
istringstream valid(input);
return (valid >> var);
}

You can check it here: [URL] The output is correct, but in my machine with C++11, MinGW 4.8 (64 bit in a 64bit-Windows8), the output is incorrect. Why?

If you want more specific info, the problem is that I use input, I think. I use std::getline(std::cin, some_string).

View 4 Replies View Related

C++ :: Member Function Undefined In Separate File

Feb 25, 2015

I have three files.

//headerfile NumArrSpecs.h
#ifndef NUMARR_H
#define NUMARR_H

[Code].....

My problem is that the storeElems member function is causing an error saying it is undefined, however there are no errors any where else in the program being reported. I have made several programs involving classes now, all with this three file format and this is the first time that a member function in the main file is being reported as undefined, so I'm not sure what to do.

View 2 Replies View Related

C++ ::  Writing A Function Template With Template Arguments?

Mar 14, 2014

I have a function:

template<class Iterator, class T>
void a(Iterator, Iterator, const T&);

and I want to be able to simplify calls to 'a' with calls like

a(someIteratableContainer);

instead of having to call:

a(someIteratableContainer.begin(), someIteratableContainer.end(), valueOfTheContainersElementType);

I also want to be able to generalize the function to handle any of the standard iteratable contains: array, vector, deque, whatever.

I was under the impression I could write:

template<template<class T> class U> a(U<T>& container) {
a(container.begin(), container.end(), g(T()));
}

where 'g()' returns an object of the element type. However, the compiler is claiming, no matter how I write a call to the overload, the original template is selected and/or the overload is invalid, depending on the various ways I attempt to write said overload.

View 7 Replies View Related

C++ :: Function That Takes Two Input Arguments And Provides Two Separate Results To Caller

Apr 20, 2014

Write a function that takes two input arguments and provides two separate results to the caller, one that is the result of multiplying the two arguments, the other the result of adding them.Since you can directly return only one value from a function, you'll need the second value to be returned through a pointer or reference parameter.

Code:
#include "Code.h"
#include <iostream>
#include <string>

double x, y;

[Code] ....

View 3 Replies View Related

C++ :: Member Function In Derived Class Call Same Function From Its Base Class?

Sep 18, 2013

How can a member function in my derived class call the same function from its base class?

View 1 Replies View Related

C++ :: Convert Each Function To A Function Template

Dec 7, 2013

I am assigned this program by my instructor and he wants me to convert the function to function template. I do not know how to do that. How to get good grades in final.

#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;
#include "Test.h"
struct char_list {
char head;

[Code] .....

View 1 Replies View Related







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