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
ADVERTISEMENT
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
Oct 7, 2014
How to initialize a static member of a class with template, which type is related to a nested class?
This code works (without nested class):
#include<iostream>
using namespace std;
struct B{
B(){cout<<"here"<<endl;}
};
template<typename Z>
[Code] ,....
View 1 Replies
View Related
Jan 15, 2015
I just happened to find that a nested private template class can be accessed directly outside the enclosing class using a using alias:
namespace ns {
class __wrapper
{
private:
[Code].....
I was hoping to see a "__wrapper::__tklass is private" error message in the first using statement as well as during the instantiation of ns::tklass, but no error is issued. I tried this on gcc-4.9.2, clang-3.5.0 and visual_studio 2013 express.
why exactly doesn't the compiler flag tklass as an error? Is it allowed by the standard? If so, wouldn't that be a serious access violation?
View 2 Replies
View Related
Sep 27, 2014
This week we are learning to use templates, and I don't understand how to call my member functions with my template based class. I tried the standard convention of calling member functions, but I keep getting an error saying name following"::" must be a class or namespace name. I'm thinking my problem lies with my typename T, but I am unsure. Line 16 is where I am getting tripped up.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
template<class T>
T Set
{
public:
[Code]...
View 2 Replies
View Related
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
Nov 24, 2013
Code:
class Main{
class B b_obj;
C c_obj=b_obj.test("some",89); // I cannot get this done.!
}
Below are .h files:
class B{
public:
C* test(const char *, int); /// this doesn't work
constructor
//member variables
[Code] ....
However, map<string, vector<bol>> test(const char *, int); works.I know second thing will work, but how to take reference of class object?
I have include .h file in main, B and C, wherever it is required.
View 4 Replies
View Related
May 14, 2012
struct emp {
char name[20];
int age;
float sal;
}e1 = {"hello", 24, 2345.67}, e2 = {"hi", 34, 45678.89};
[Code] ....
Query: I am unable to get emp &fun() and fun() = e2...
View 3 Replies
View Related
Dec 3, 2014
How to assign the array in object Q to the 12 months, Dec through Jan. I then have to sort the array and display the strings from Jan to Dec.
// main.cpp
#include <iostream>
#include <string>
#include <cstring>
#include <ctime>
#include <algorithm>
using namespace std;
// Declare template class
template <class T, int n>
[Code] ....
View 8 Replies
View Related
Feb 2, 2014
I'm trying to get the command pattern for template classes down. I'm just having a hard time implementing one part of the code. I can't figure out why both the method being passed and the type accepted are not the same type.
SimpleCommand class
template <class Receiver>
class SimpleCommand : public Command {
public:
typedef void(Receiver::*Action)();
SimpleCommand(Receiver *r, Action a) :
[code].....
And their instantiation
MyClass *receiver = new MyClass;
Command *aCommand =
new SimpleCommand<MyClass>(receiver, &MyClass::Action);
aCommand->Execute();
View 7 Replies
View Related
Apr 6, 2014
I got an array class, but I'm getting this error:
Error1error C2953: 'Array2D' : class template has already been defined
Here's the code:
#include "Tools.h"
template <typename T>
class Array2D {
T** arr;
int xSize;
int ySize;
[Code] .....
View 3 Replies
View Related
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
Feb 7, 2013
I'm trying to implement a vector class in C + +. However when I go to actually run the main, it generates an interrupt operating system.
MAIN.cpp
#include "header.h"
int main() {
// Definisco le istanze delle classi
vettore <int> vet;
vettore <int> vet2;
vettore <int> vet3;
[code].....
View 7 Replies
View Related
Apr 16, 2014
this is a piece of code from my program for a game of quiz...this class is supposed to manage the marks of a player. i am passing marks for a question depending on its correctness to the setscore function and i want it to keep on updating the value of m_score so that finally i can return it from the getscore function....with this piece of code i am getting some junk values being returned by the getscore function .
class Player
{
private:
char* m_name;
int m_score;
public:
friend class Scoremanager;
[code]....
View 6 Replies
View Related
Aug 21, 2013
I am writing a program which is using SDL library. I have two different classes which one of them is Timer Class and the other is EventHandling Class.
I need to use some member functions and variables of Timer in some Eventhandling Class member functions, Although I want to define an object of Timer in int main {} and relate it to its member function that has been used in Eventhandling member function in order that it becomes easier to handle it, I mean that I want to have for example two objects of timer and two objects of Eventhandling class for two different users.
I do not know how to relate an object of a class from int main{} to its member function which is being used in another class member function.
Lets have it as a sample code:
class Timer {
private:
int x;
public:
Timer();
get_X();
start_X();
[Code] ....
View 4 Replies
View Related
Mar 5, 2014
template <class T>
void Arreglo<T> :: Registro (ifstream& Entrada) {
Entrada >> Cantidad;
Dealer = new T [Cantidad];
for (int i = 0; i < Cantidad; i++) {
[Code] .....
It says the following error when I comile it:
error: expected type-specifier before 'Detail'
(*(Dealer + i)).Modelo = new Detail[(*(Dealer + i)).AmountModels];
error: expected ';' before 'Detail'
View 3 Replies
View Related
Apr 10, 2014
I have been trying to compile this program for 3 days. he subject is my error. I know it has something to do with including the cpp file with the header file however my textbook says this is what I must do in order to use templates. I have tried 2 compilers Code Blocks and Microsoft Visual C++ express 2010.
Here is my code:
header file
#pragma once
#ifndef _NODE
#define _NODE
template<class ItemType>
class Node {
[Code] ....
View 9 Replies
View Related
Nov 29, 2012
The below code compiles without error using VS 2012 but with g++ 4.1.2 I get this error:
Code:
main.cpp: In function 'int main(int, char**)':
main.cpp:37: error: no matching function for call to 'StringHelper::stringToNumeric(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
Here is the code:
#include <string>
#include "boost/lexical_cast.hpp"
using boost::lexical_cast;
using boost::bad_lexical_cast;
class StringHelper {
[Code] ....
This is part of a larger program so in reality StringHelper has more static functions but this example does produce the error the same as the code when in the larger program.
To get it to compile under g++ I had to assign the return value from substr() to a string and pass that string to stringToNumeric. Why do I have to do this for g++ and not VS? Do I have something wrong with my template function that g++ is calling out and VS is not?
View 4 Replies
View Related
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
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
View Related
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
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
Oct 21, 2013
I mount a function (parameter - numeric vector; returns a string). However, this same function is used in several classes. To avoid that I keep duplicating the same code within these classes there is a way to do that as the code below?
std::string func( const vector<int> vec ) {
//processamento
return result;
} class A {
[Code] ....
View 6 Replies
View Related
Jun 16, 2013
whether i can define a member function inside a class or not in C++. Is the following code is legal?
#include<iostream> using namespace std;
class adder {
private:
int a;
int b;
int c;
int answer;
public:
[code]....
View 6 Replies
View Related
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
Sep 17, 2013
CODE:
class Secure {
private:
int seconds;
bool isRUNNING;
public:
Secure(int seconds) {
[Code] .....
ERROR:
error C2276: '&' : illegal operation on bound member function expression
I read that due to explicit casting, threads cannot be created within a class. I'm trying to thread a scanning system to relieve stress on my main program/module, rather than having the scanner stunt their performance.
View 4 Replies
View Related