C++ :: Template Specialization Does Not Work For User-defined Object

May 23, 2014

Can you take a look why I am getting compile error.

Code:
// clang++ main.cpp -g -std=c++11 -o main
#include <iostream>
class QuoteClass {
public:
QuoteClass() = default;
QuoteClass(std::string param) {symbol = param;}
std::string get_symbol() {return symbol;}

[Code] ....

View 5 Replies


ADVERTISEMENT

C++ :: Partial Template Specialization With Template Class

May 27, 2013

I have a generic template class with another template in one of its types. Now I want to specialize one of its methods for a particular (template) class, which leads to a compile error, however.

Here is the example:

#include <stdio.h>
template<typename Type>
class Obj1 {
public:
void ID() { printf("Object 1, size = %zu

[Code] .....

GCC ends with:
:35:27: error: type/value mismatch at argument 2 in template parameter list for ‘template<class Type, template<class> class O> class Foo’
:35:27: error: expected a class template, got ‘Obj2<Type>’

What is wrong with the specialization? Can it even be achieved and how (if so)?

View 1 Replies View Related

C++ :: How To Assign Char Array As User Defined String Object

Apr 17, 2014

STRING s1 = “FOOBAR”

Here STRING is a user defined class. how to assign a constant char array "FOOBAR" to string object? Copy constructor need to be same class type as parameter. and overloading assignment operator also need to be same class type. I think 'friend' can let pass another type of object rather than STRING to do operation on overloaded '=' operator. How could it be done if it is possible at all?

View 2 Replies View Related

C++ :: Object And Classes - Declaration Of Variable Using User Defined Type

Mar 17, 2013

I am getting a compilation error from the code below. It is when i am naming a variable with my user defined type.

#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
class person {

[Code] .....

C:Dev-CppTRIAL.PASS.!!!.cpp In function `int main()':
66 C:Dev-CppTRIAL.PASS.!!!.cpp expected primary-expression before "p"
66 C:Dev-CppTRIAL.PASS.!!!.cpp expected `;' before "p"
74 C:Dev-CppTRIAL.PASS.!!!.cpp `p' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
83 C:Dev-CppTRIAL.PASS.!!!.cpp `X' undeclared (first use this function)

View 4 Replies View Related

C++ :: Use External Template With Template Specialization?

Nov 17, 2013

[URL]

#include <iostream>
struct Outer {
template<typename T>
void go() {
std::cout << Test<T>::f() << std::endl;

[Code] .....

I have tried several variants on this code to no avail. Outer is in a header, along with the extern template statements, and the specializations after main are in their own cpp file. Main is in a different cpp file.

What do have to do to make this work? I cannot bring the definitions of f() into the header, and they will be different for different template parameters. Ideally, I want Test to remain a private member of Outer, though this can change if it's the only option.

View 1 Replies View Related

C++ :: Nested / Recursive Template Specialization

Feb 11, 2014

I'm trying to write some naive binary serialization code and wanted to cut down on repetition of logic for serializing/deserializing nested vectors or other STL containers to reduce the chance of typos etc, and thought templates might hold a solution for me.

Code:
template <typename T> void serializeField(IWriter& writer, const T& val) {
writer.write((char*)&val, sizeof(T));
}

template<typename U, typename V>
template <> void serializeField(IWriter& writer, const U<V>& collection)

[Code] ....

Is there a way to do something like this? It isn't a big deal for me to just manually write code to serialize my vectors to the needed depth, but it sure would be nice to get this working.

View 10 Replies View Related

C++ :: Partial Specialization Of Template Parameter

Nov 20, 2013

I've been trying to create a templated class that takes a template as a parameter. I'd like to specialise this class for certain partial specializations of the template parameter but can't seem to figure out how to do it nor find anything online, (although I may be searching for the wrong thing).

As an example, say I have a class A that takes a template class with two parameters as its parameter:

template< template<class X, class Y> class Z > class A {};

I'd like to have a general version of A, for a general version of Z, but a specialisation of A for a specialisation of Z, e.g. where X is int but Y is still any type.

View 6 Replies View Related

C++ :: Not Picking Template Partial Specialization?

Nov 18, 2013

[URL]

#include <iostream>
#include <type_traits>
template<typename T, typename = void>
struct Test {
static int constexpr value = 1;

[Code] .....

Why does it output 1 instead of 2? How can I make it output 2 and still output 1 for e.g. Test<double>::value?

View 3 Replies View Related

C++ :: Template Specialization For Char Arrays

Jun 8, 2012

I'm trying to get template specializations working for char * as well as for char[]. I.e. in the following code the last test does not use the specialization and fails:

Code:
#include <string>
#include <iostream>
#include <cstring>
template<typename T1, typename T2>
bool compare(const T1& lhs, const T2& rhs) {

[Code] ....

View 4 Replies View Related

C++ :: Template Partial Specialization - Combining Two Types Of Resources

May 15, 2013

I want to write a template that combines two type of resources:

class someClasses {
typedef someType ElementType;
} template<class T1,class T2>
class combo {
...
}

And I want to specify my T1 and T2 has the same ElementType, how can I write my combo class to partial specialize the general case so the ElementType check is at compile time?

View 9 Replies View Related

C/C++ :: Template Specialization Of Single Method From Templated Class?

Jan 21, 2013

I want to specialize a particular function for Integer datatype inside a class template. My XX.h will be

namespace ZZ {
       template <class T>
       class XX {
           void doSomething(T x);
       };
}  

provide me XX.cpp which has template specialization for the function doSomething on Integer datatype.

I tried the following in XX.cpp

#include "XX.h"
using namespace ZZ;  
template <class T>
void XX<T>::doSomething(T x) {

[Code] ...

But this code is not working.

View 2 Replies View Related

C++ :: Floating Point Template Parameters (called Specialization)

Sep 12, 2012

This code snippet just won't compile in a conforming compiler:

Code:
template<> struct series<0.0, 0, 0>

Because of the floating point template parameter.... If this "specialization" is useful, how come MS would have discarded it?

View 4 Replies View Related

C++ :: Array 2D - Class Template Has Already Been Defined Error

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

C/C++ :: Error C2995 Function Template Has Already Been Defined

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

C++ :: Matrix Class - Template Constructor Doesn't Work So Well

Feb 5, 2013

I realized a Matrix class to practice and I have a problem I can not solve! Here my problematic code:

Mtrx.h:

Code:
template <class T>
Mtrx::Mtrx(dim m, dim n, const bool random_constructed = false, const T min = static_cast<T>(0), const T max = static_cast<T> (10))
Mtrx.C

[Code] ...

And here the relative main section:

Code:
Mtrx rand1 ( 5, 5, bool);// ok
cout<<rand1<<endl;

Mtrx rand2 ( 7, 3, bool, -5, 20);// ok
cout<<rand2<<endl;

Mtrx rand3 ( 7, 7, bool, 0., 15.);// compilation error: undefined reference to
// "Mtrx::Mtrx<double>(unsigned long, unsigned, bool, double, double)"
// collect2: error: ld returned 1 exit status

I compiled in a Linux OS with g++ -std=c++11

View 3 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 Sharp :: How To Make Object In Form1 Work In Form2

May 20, 2012

I've got a calendar in Form 1 in my code, and buttons in Form2. I need these buttons to send codes for this calendar in Form1. How may I make these work? My friend told me to do a function as a new class, but I'm not sure how can I do this.

View 5 Replies View Related

C :: User Defined Array Size

Jul 23, 2014

The instructions call for the user to define the size of the array and all I have ever done is use a predefined size for the array and then let the user fill it. Here is what I have so far:

Code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void Display (void);
void random (int *, int);
void Ascending (int *, int);
void Descending (int *, int);

[code]....

View 7 Replies View Related

C++ :: STL Set Orderinf User Defined Objects

Mar 4, 2014

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

class person{
int age;

[Code] ....

When I executed the above code it displays

Age is 37
Age is 25

Here I have defined operator< so that set can identify the order of person objects. I am clueless how set could identify that

person p[3]={person(25),person(37),person(25)};

I have provided one duplicated object with age 25

I havent overloaded == operator also,then how it could identify?

View 3 Replies View Related

C++ :: Built In Functions And User Defined?

Feb 25, 2015

what is a built in function and user defined is cause i cant seriously find a decent example about it in the net

View 4 Replies View Related

C :: Return Value From User Defined Function To Main

Nov 14, 2014

I want to return value from user defined function to main.

Code:

#include <stdio.h>
int mult ( int x, int y );
int add(int x, int y);
int loop (int y);
int main() {

[Code] ......

View 1 Replies View Related

C :: User Defined Number Of Decimal Places?

Sep 2, 2013

I need the user to be able to input the number of decimal places they wish to have displayed in the output. Everything works fine as is, I just don't know how to allow for the user to input the number of decimal places they want the output to have.

Code:

#include<stdio.h>
#include<math.h>
#define PI 3.141592654
int main(void) {
//Local Declarations
int x; //desired number of decimal places
float radius; //radius of circle
float circumference; //circumference of circle

[Code] .....

View 2 Replies View Related

C++ :: User Defined Functions - Type Name Is Not Allowed

Dec 5, 2014

So I received an error of "type name is not allowed" and I don't know how to fix this.... Here is the code:

retrieveData(string Name, double wage, double hours, int exemptions, char Status);
dataCalculations(double wage, double hours, int exemptions, char Status, double grossPay, double ficaTax, double incomeTax, double netPay, double taxWithheld);
sendData(string Name, int ID, double hours, double wage, double ficaTax, double incomeTax, double grossPay, double netPay);

View 9 Replies View Related

C++ :: Bubble Sorting With User Defined Amount Of Elements

Feb 25, 2014

I am writing some code to bubble sort a list of randoms numbers, 10 at the moment. I want to be able to user input the amount of elements that would be sorted. I think i'd have to use vector instead of arrays, but unsure how to do this. Or, how to be able to change the size of the array.

#include <iostream>
using namespace std;
#define SIZE 10

float numbers[SIZE];
int move_n;

[Code] ....

View 1 Replies View Related

C++ :: Binary Search Tree Used With User Defined Class

Nov 29, 2014

I am working on a program that uses a class I created called Student. I want to be able to add different students to a Binary Search Tree, and use the student's gpa (grade point average) to compare students with each other and place them in the correct location in the Tree.

Student class:

#ifndef STUDENT_H
#define STUDENT_H
#include <string>
#include <iostream>
#include <iomanip>
class Student{
friend ostream& operator<<(ostream& out, const Student& stu);

[Code] ......

View 3 Replies View Related

C# :: For Loops To Make User Defined V Shape Output

Jul 29, 2014

Write a program to ask a user to input a symbol from the keyboard and to output that symbol in a n X n/2 sized V where n = the width of the V. You must use a loop to process the data

I am stuck at trying to figure out how to do the actual output formatting. This is where I am sitting currently.

string character = "";
int vheight = 0;
Console.WriteLine("Enter the character you wish to use for your V: ");

[Code]....

so Im really just a bit stumped on how to get the actual V shape to be formatted..

View 14 Replies View Related







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