13569

shared_ptr template argument invalid

Question:

I am trying to have a static method return a shared_ptr.

It is not compiling and is giving template argument 1 is invalid.

I can not figure out why this is.

Also, stack overflow says my post is mostly code and that I should add more detail. I don't know why this is, as being concise never hurt anyone. My problem is clear cut and and can be detailed easily.

Compiler error

src/WavFile.cpp:7:24: error: template argument 1 is invalid std::shared_ptr<WavFile> WavFile::LoadWavFromFile(std::string filename)

WavFile.cpp

#include "WavFile.h" #include "LogStream.h" #include "assert.h" using namespace WavFile; std::shared_ptr<WavFile> WavFile::LoadWavFromFile(std::string filename) { ifstream infile; infile.open( filname, ios::binary | ios::in ); }

WavFile.h

#pragma once #ifndef __WAVFILE_H_ #define __WAVFILE_H_ #include <fstream> #include <vector> #include <memory> namespace WavFile { class WavFile; } class WavFile::WavFile { public: typedef std::vector<unsigned char> PCMData8_t; typedef std::vector<unsigned short int> PCMData16_t; struct WavFileHeader { unsigned int num_channels; unsigned int sample_rate; unsigned int bits_per_sample; }; static std::shared_ptr<WavFile> LoadWavFromFile(std::string filename); private: WavFile(void); private: WavFileHeader m_header; PCMData16_t m_data16; PCMData8_t m_data8; }; #endif

Answer1:

Change the namespace name to something else. Now it clashes with the class' name, since you are using namespace WavFile;. Simple example that illustrates the error:

#include <iostream> #include <memory> namespace X // changing this to Y makes the code compilable { class X{}; } using namespace X; // now both class X and namespace X are visible std::shared_ptr<X> v() // the compiler is confused here, which X are you referring to? { return {}; } int main() {}

If you insist to have the namespace named as the class, then get rid of the using namespace X; and qualify the type, std::shared_ptr<WafFile::WavFile>.

Recommend

  • Adding silence between words in audio file using ffmpeg
  • how to divide an audio file (mp3 or wav) into smaller ones at silent segments
  • undefined reference error due to use of static variables [duplicate]
  • Opaque reference instead of PImpl. Is it possible?
  • runtime-check whether an instance (Base*) override a parent function (Base::f())
  • apply a javascript function to draggable copy
  • Building Qt project for C++11 standard
  • C++ friend class std::vector
  • Aptana 3 remove bundle (jquery)
  • netsh acl setting (need alternative method - registry settings?)
  • Error in installing package: fatal error: stdlib.h: no such file or directory
  • Compiling dlib on OS X
  • Using $this when not in object context
  • Array.prototype.includes - not transformed with babel
  • Volley JsonObjectRequest send headers in GET Request
  • Accessing IRQ description array within a module and displaying action names
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Build own AppleScript numerical error handling
  • Websockets service method fails during R startup
  • Google cloud sdk not working when python points python3
  • AT Commands to Send SMS not working in Windows 8.1
  • Is there a mandatory requirement to switch app.yaml?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • C# - Getting references of reference
  • Hits per day in Google Big Query
  • -fvisibility=hidden not passed by compiler for Debug builds
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Linking SubReports Without LinkChild/LinkMaster
  • apache spark aggregate function using min value
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • Sorting a 2D array using the second column C++
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal