質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

解決済

3回答

6827閲覧

std::vector<int> 型のテンプレートクラスが作れない

退会済みユーザー

退会済みユーザー

総合スコア0

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2020/12/25 12:16

提示コードですが以下のエラーを解決することが できずに困っています。何とかをしていせんとかと言われているので簡単なミスにも思えますがどうしてもコンパイルエラーを直せません。どうすればstd::vector<int>型のテンプレートクラスが作れるのでしょうか?ソースファイル側でエラーがでます。int型ではエラーなしに実行できました。

エラー[
クラス型のテンプレート パラメーターは、構造クラス型である必要があります
]

エラー[
名前の後に '::~' を付けることができるのはクラス名または名前空間名だけです
]

エラー[
クラス テンプレート "sample" の引数リストがありません
]

cpp

1#include "Header.hpp" 2#include <iostream> 3#include <vector> 4 5test::test() 6{ 7 printf("test\n"); 8} 9 10void test::f() 11{ 12 printf("fff\n"); 13} 14 15 16 17template<std::vector<int>> 18sample::sample(std::vector<int> a):test() 19{ 20 printf("sample"); 21} 22 23void sample::f() 24{ 25 printf("えええ\n"); 26}

hpp

1#include <iostream> 2#include <vector> 3 4class test 5{ 6public: 7 8 test(); 9 void f(); 10}; 11 12template<class type> 13class sample : private test 14{ 15public: 16 sample(type a); 17 18 void f(); 19 20}; 21 22 23 24 25template<> 26class sample<std::vector<int>> : private test 27{ 28public: 29 sample(std::vector<int> a); 30 31 void f(); 32 33};

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2020/12/25 23:49

解決したのですがstd::vectorを使うとさっきの質問では解決しないため質問しました。
episteme

2020/12/26 00:44

std::string を std::vector<int> に取り替えたらエラー? そんなはずはないんだが。
退会済みユーザー

退会済みユーザー

2020/12/26 00:46

そうですかね?正確にはstd::vector<Game::Vertex>>; という構造体のベクターなんのですが。 ソース部の書き方もあってますでしょうか?前の質問で
episteme

2020/12/26 00:51 編集

Game::Vertex が何者かわからんので答えようがない てかあなた、ベストアンサーつけたやんか。
guest

回答3

0

ベストアンサー

C++

1#include <iostream> 2#include <vector> 3 4class test { 5public: 6 test(); 7 void f(); 8}; 9 10template<class type> 11class sample : private test { 12public: 13 sample(type a); 14 void f(); 15}; 16 17template<> 18class sample<std::vector<int>> : private test { 19public: 20 sample(std::vector<int> a); 21 void f(); 22}; 23 24test::test() { printf("test\n"); } 25void test::f() { printf("fff\n"); } 26 27template<class type> 28sample<type>::sample(type a) { printf("sample<type>\n"); } 29template<class type> 30void sample<type>::f() { printf("sample<type>::f\n"); } 31 32 33sample<std::vector<int>>::sample(std::vector<int> a):test() { printf("sample<std::vector<int>>\n"); } 34void sample<std::vector<int>>::f() { printf("えええ\n"); } 35 36int main() { 37 sample<int> sample_int(123); 38 sample_int.f(); 39 40 std::vector<int> v; 41 sample<std::vector<int>> sample_vector(v); 42 sample_vector.f(); 43}

投稿2020/12/26 00:45

編集2020/12/26 00:59
episteme

総合スコア16614

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

テンプレートを用いたクラスや関数を書く時明示的実体化でも行っていない限り
ヘッダーに実装も書きこんで下さい。

c++

1// Hearder.hpp 2#pragma once 3 4#include <iostream> 5#include <vector> 6 7struct Test 8{ 9 Test() 10 { 11 std::cout << "親クラスのTestのデフォルトコンストラクタ" << std::endl; 12 } 13 14 virtual void f() 15 { 16 std::cout << "親クラスのfメソッド" << std::endl; 17 } 18}; 19 20template<class Type> 21struct Sample:public Test 22{ 23 explicit Sample(const Type a) 24 { 25 std::cout << "子クラスのSample<type>のコンストラクタ" << std::endl; 26 } 27 28 void f()override 29 { 30 std::cout << "Sample<type>クラスでオーバーライドしたfメソッド" << std::endl; 31 } 32}; 33 34template<> Sample<std::vector<int>>::Sample(const std::vector<int> a) 35{ 36 std::cout << "子クラスのSample<std::vector<int>>のコンストラクタ" << std::endl; 37} 38 39template<> void Sample<std::vector<int>>::f(){ 40 std::cout << "Sample<std::vector<int>>クラスでオーバーライドしたfメソッド" << std::endl; 41}

c++

1// main.cpp 2#include "./Hearder.hpp" 3#include <vector> 4 5int main() 6{ 7 int int_v = 0; 8 auto vec_int_v = std::vector<int>(); 9 10 auto s1 = Sample<int>(int_v); 11 auto s2 = Sample<std::vector<int>>(vec_int_v); 12 13 s1.f(); 14 s2.f(); 15 return 0; 16}

wandboxで実行

ちなみにc++17を使える環境なら
if constexprで特殊化の所を書かずにコードを簡略化可能です。

c++

1// Hearder.hpp 2#pragma once 3 4#include <iostream> 5#include <vector> 6#include <type_traits> 7 8struct Test 9{ 10 Test() 11 { 12 std::cout << "親クラスのTestのデフォルトコンストラクタ" << std::endl; 13 } 14 15 virtual void f() 16 { 17 std::cout << "親クラスのfメソッド" << std::endl; 18 } 19}; 20 21template<class Type> 22struct Sample:public Test 23{ 24 explicit Sample(const Type& a) 25 { 26 if constexpr(std::is_same_v<std::vector<int>, Type>) 27 std::cout << "子クラスのSample<std::vector<int>>のコンストラクタ" << std::endl; 28 else std::cout << "子クラスのSample<type>のコンストラクタ" << std::endl; 29 } 30 31 void f()override 32 { 33 if constexpr(std::is_same_v<std::vector<int>, Type>) 34 std::cout << "Sample<std::vector<int>>クラスでオーバーライドしたfメソッド" << std::endl; 35 else std::cout << "Sample<type>クラスでオーバーライドしたfメソッド" << std::endl; 36 } 37};

wandboxで実行

投稿2020/12/25 14:56

編集2020/12/26 07:19
退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2020/12/26 00:39

質問ですが明示的具体化をしたいのですがどうすればいいのでしょうか?サンプルコードをお願いできますか?
退会済みユーザー

退会済みユーザー

2020/12/26 06:14

少しコードが長くなってしまったのでwandboxに書きました。 https://wandbox.org/permlink/Sy9gypxMz0PdzfLr 上記urlの先のsample.cppファイルをご覧ください。 そのファイルの最後の二行に実体化を行う部分が記載されています。 しかし明示的実体化を行う場合 template class Sample<int>;// intで実体化 template class Sample<std::vector<int>>;// std::vector<int>で実体化 のように使用する型をわざわざ列挙せねばならず面倒なので ヘッダーに実装も書いてしまう事をおすすめします。
guest

0

前回質問でepistemeさんが書かれたコードはわからなかったということでしょうか…

diff

1-template<std::vector<int>> 2-sample::sample(std::vector<int> a):test() 3+sample<std::vector<int>>::sample(std::vector<int> a) : test() 4{ 5 printf("sample"); 6} 7 8-void sample::f() 9+void sample<std::vector<int>>::f() 10{ 11 printf("えええ\n"); 12}

投稿2020/12/25 14:03

編集2020/12/25 14:07
SHOMI

総合スコア4079

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2020/12/25 23:56

わかったのですがstd::vectorになるとエラーになるので手におえなくなりましたw
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問