I have an LNK2019 error that has something to do with how I am setting up a function that takes a list&amp; parameter with template.  Is there a proper way to set up a list as a parameter like this?  I thought this would be a neat way to reduce lines of code for multiple classes using the same functionality.  I'm sure this is a duplicate question so I apologize, I have not yet found an answer. Helpers.h: #pragma once #ifndef HELPERS_H #define HELPERS_H #include &lt;list&gt; #include &lt;string&gt;  void stringRemoveCommas(string&amp; str); void stringToUpperAll(string&amp; str); template&lt;class T&gt; bool doesItemExist(string&amp; str, list&lt;T&gt;&amp; lst); // POI #endif // !HELPERS_H  Helpers.cpp template&lt;class T&gt; bool doesItemExist(string&amp; str, list&lt;T&gt;&amp; lst) {     for (auto it = lst.begin(); it != lst.end(); ++it)     {         if (it-&gt;getName() == str)             return true;     }     return false; }  ObjectList.cpp: #include &quot;ObjectList.h&quot;           // includes &quot;Helpers.h&quot; bool addObject(string&amp; str, list&lt;Object&gt;&amp; lst) {     stringRemoveCommas(str);      // works fine     stringToUpperAll(str);        // works fine     if (!doesItemExist(str, lst)) // unresolved external symbol     {         Object obj(str);         lst.push_back(obj);         return true;     }     return false; }  result: error LNK2019: unresolved external symbol &quot;bool __cdecl doesItemExist(class std::basic_string&lt;char,struct std::char_traits,class std::allocator &gt; &amp;,class std::list&lt;class Object,class std::allocator &gt; &amp;)&quot; (??$doesItemExist@VObject@@@@YA_NAAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@AAV?$list@VObject@@V?$allocator@VObject@@@std@@@1@@Z) referenced in function &quot;bool __cdecl addObject(class std::basic_string&lt;char,struct std::char_traits,class std::allocator &gt; &amp;,class std::list&lt;class Object,class std::allocator &gt; &amp;)&quot; (?addObject@@YA_NAAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@AAV?$list@VObject@@V?$allocator@VObject@@@std@@@2@@Z) 1&gt;D:\VS_Projects\Object Manager\Debug\Object Manager.exe : fatal error LNK1120: 1 unresolved externals
341
Quote from :     The only portable way of using templates at the moment is to implement them in header files by using inline functions.   Why is this?  (Clarification: header files are not the only portable solution. But they are the most convenient portable solution.)
56847
This is an extension of .  So, using the answer from the previous question I mentioned, I am able to make a generic utility that would convert/parse an Enumeration to a String.  Now the only problem I have with my solution is, I can't seem to refactor the code to my liking. It works the way I want it to, but it is a tad more on the code heavy side than I had hoped for. I was curious if there was a design pattern (or any other way to code it) to help solve this issue. I am thinking there is a creational design pattern that will help in this case. I would be easy if I had lambda expression but, Apex is not quite there yet ;).  The code I use has kind of patterns dug into it already. It uses the Strategy, Factory, and Template(ish) pattern I believe. I have the resources to find something to solve this issue (my favorite Design Pattern books), but nothing I can think of jumps out at me.  Here is the code for my utility for just one enumeration:  private interface EnumerationToStringHelper {     List&lt;Object&gt; ProvideEnumerationValues();     Boolean CompareEnumerationToString(String possibleEnumeration, Object enumerationValue); }  private class ExampleEnumToStringHelper implements EnumerationToStringHelperExampleEnum {     public List&lt;Object&gt; ProvideEnumerationValues(){ return (List&lt;Object&gt;)ExampleEnum.values(); }     public Boolean CompareEnumerationToString(String possibleEnumeration, Object enumerationValue)     {         return  ((ExampleEnum)enumerationValue).name().equalsIgnoreCase(possibleEnumeration);     } }  private static Map&lt;Type, EnumerationToStringHelper&gt; TypeToEnumerationMap;  private static void PopulateTypeToEnumerationMap() {     TypeToEnumerationMap = new Map&lt;Type, EnumerationToStringHelper&gt;     {         Type.forName('ExampleEnum') =&gt; new ExampleEnumToStringHelper()     }; }  public static Object ConvertStringToEnumeration(Type enumerationType, String possibleEnumeration) {     if(possibleEnumeration == null) return null;     if(TypeToEnumerationMap == null)         PopulateTypeToEnumerationMap();      EnumerationToStringHelper conversionAction = TypeToEnumerationMap.get(enumerationType);      if(conversionAction == null)         throw new EnumerationToStringException('The Type you enter is either a) not an enumeration or b) not a supported enumeration.');      for(Object particalarEnumValue : conversionAction.ProvideEnumerationValues())         if(conversionAction.CompareEnumerationToString(possibleEnumeration, particalarEnumValue))             return particalarEnumValue;     return null; }   public class EnumerationToStringException extends Exception { }   Now in order to use the utility, it is exactly what I hoped for.  return (ExampleEnum)RT_GeneralUtilities.ConvertStringToEnumeration(Type.forName('ExampleEnum'), 'Testing');   It is a little long, but it beats having the entire for loop for 12 or more different Enum types. That is why I wanted to build the utility - essentially for better readability and test coverage purposes.  The downside with this is I have to make a helper for every single Enum type. So it makes it kind of an eyesore maintainability-wise.    I was hoping there was a pattern I could implement to assuage that eyesore and make it appealing on both sides. Any suggestions?
1725
I have a generic method with this (dummy) code (yes I'm aware IList has predicates, but my code is not using IList but some other collection, anyway this is irrelevant for the question...)  static T FindThing&lt;T&gt;(IList collection, int id) where T : IThing, new() {     foreach T thing in collecion     {         if (thing.Id == id)             return thing;     }     return null;  // ERROR: Cannot convert null to type parameter 'T' because it could be a value type. Consider using 'default(T)' instead. }   This gives me a build error      "Cannot convert null to type parameter   'T' because it could be a value type.   Consider using 'default(T)' instead."   Can I avoid this error?
31525
I've stumbled upon a problem at the final stage of a new feature I was building (as always)   I need to pass in a custom string into a salesforce template, however there doesnt seem to be a way of doing this through the standard methods.   I'm passing in a contact ID, to email the correct user and name to personailse the email. But I need a string to be passed to the template which is completely unrelated to the contact record, and need to be dynamic. So I cant just put it in the template.   Is there a way? I know i cant pass in a custom object as it needs to be a contact, user or lead id to email a user.   Can you help?   Here is the code if you need to see it:  public static void sendSingleMail(id objId, ID templateId, string fromaddress) {      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();     mail.setReplyTo(fromaddress);     mail.setTemplateId(templateId);     mail.setTargetObjectId(objId);     mail.saveAsActivity = false;     mail.setReplyTo(fromaddress);     mail.setSenderDisplayName(fromaddress);      ErrLogger.logger('Email being sent to :');     ErrLogger.logger('objId found:  ' + objId);     ErrLogger.logger('templateId found:  ' + templateId);     ErrLogger.logger('fromaddress found:  ' + fromaddress);      Messaging.sendEmail(new Messaging.SingleEmailmessage[] {mail}); }
42083
What are the C++ coding and file organization guidelines you suggest for people who have to deal with lots of interdependent classes spread over several source and header files?  I have this situation in my project and solving class definition related errors crossing over several header files has become quite a headache.
366
Let's say we have this code for a LWC:  &lt;!-- helloExpressions.html --&gt; &lt;template&gt;     &lt;div class="slds-m-around_medium"&gt;         &lt;lightning-input name='firstName' label="First Name" onchange={handleChange}&gt;&lt;/lightning-input&gt;         &lt;lightning-input name='lastName' label="Last Name" onchange={handleChange}&gt;&lt;/lightning-input&gt;         &lt;p class="slds-m-top_medium"&gt;Uppercased Full Name: {uppercasedFullName}&lt;/p&gt;     &lt;/div&gt; &lt;/template&gt;   and this is the JS file:  // helloExpressions.js import { LightningElement, track } from 'lwc';  export default class HelloExpressions extends LightningElement {     @track firstName = '';     @track lastName = '';      handleChange(event) {         const field = event.target.name;         if (field === 'firstName') {             this.firstName = event.target.value;         } else if (field === 'lastName') {             this.lastName = event.target.value;         }     }      get uppercasedFullName() {         return `${this.firstName} ${this.lastName}`.toUpperCase();     } }   firstName and lastName have been decorated with @track. Now as per the documentation, decorating a property with @track makes it private and reactive, while to mark a property public and reactive, use @api.  So if I just change the decorator to @api for both these variables, it should work. I am referring these from the same component only. But it's not working.  UPDATE: 27th Feb -------------------- Somehow it's working today. I just changed the decorator of the variables to api.  import { LightningElement, api } from 'lwc';  export default class HelloExpressions extends LightningElement {     @api firstName = '';     @api lastName = '';      handleChange(event) {         const field = event.target.name;         if (field === 'firstName') {             this.firstName = event.target.value;         } else if (field === 'lastName') {             this.lastName = event.target.value;         }     }      get uppercasedFullName() {         return `${this.firstName} ${this.lastName}`.toUpperCase();     } }   So it's working.
47824
I've seen some examples of C++ using template template parameters (that is templates which take templates as parameters) to do policy-based class design. What other uses does this technique have?
7923
template&lt;typename T, size_t n&gt; size_t array_size(const T (&amp;)[n]) {     return n; }   The part that I don't get is the parameters for this template function. What happens with the array when I pass it through there that gives n as the number of elements in the array?
99300
So, no matter what I seem to do, I cannot seem to avoid having Dev C++ spew out numerous Multiple Definition errors as a result of me including the same header file in multiple source code files in the same project.  I'd strongly prefer to avoid having to dump all my source code into one file and only include the header once, as that's going to make my file very long and difficult to manage.  Essentially, this is what's going on:  #ifndef _myheader_h #define _myheader_h  typedef struct MYSTRUCT{ int blah; int blah2; } MYSTRUCT;  MYSTRUCT Job_Grunt; MYSTRUCT *Grunt = &amp;Job_Grunt; MYSTRUCT Job_Uruk; MYSTRUCT *Uruk = &amp;Job_Grunt;  int Other_data[100];  void load_jobs();  #endif   Example Cpp File (They pretty much all look something like this):  #include "myheader.h"  void load_jobs(){  Grunt-&gt;blah = 1; Grunt-&gt;blah2 = 14;  Uruk-&gt;blah = 2; Uruk-&gt;blah2 = 15;  return; }   Bear in mind that I have about 5 cpp files that include this one header, each one dealing with a different type of struct found in the header file.  In this example there was only the one struct containing a couple of members, when there are about 4-6 different structs with many more members in the actual header file.  All the files I've included it in follow the same formula as you see in this example here.  Now I understand that the header guard only stops each individual cpp file from including the header file more than once.  What would seem to be happening is that when the compiler reads the include at the start of each cpp, it defines the header file all over again, which is causing it to spit out lines and lines of:  Multiple Definition of Uruk, first defined here   Multiple Definition of Job_Uruk, first defined here   Multiple Definition of Grunt, first defined here   Multiple Definition of Job_Grunt, first defined here   Multiple Definition of Other_data, first defined here   I'll see a set of this for just about every cpp file in the project which includes the header.  I've tried moving the definitions of the struct and the struct variables to the cpp files, but then the other cpp files cannot see them or work with them, which is very important as I need all files in the project to be able to work with these structs.  But the single most confusing part about this problem requires a little more explanation:  The way I'm setting up these multiple files in this project is identical to the book I'm working with, All In One Game Programming by John S. Harbour.  I ran into the exact same problems when I created the files for example projects in the book which called for one header included by multiple cpps in the same project.  I could type them out, word for word from the book, and I do mean word for word... and I'd get the series of MD errors for every cpp in the project.    If I loaded the example project from the CD included with the book, it would compile and run without a problem, allthough the files themselves, as well as the project options, were by all appearances identical to the ones I had created.  If I created my own project file, and simply added the source and header files for the example project from the CD, this, too, would also compile and run, though I can find no difference between those and mine.  So then, I tried making my own project file, then creating the blank source and header files and adding them to it, and then filling them by copying and pasting their contents from the files on the CD they were meant to correspond to(the same ones that had worked). And sure enough, I'd get the same thing...lines and lines of MD error messages.  I'm absolutely baffled.  I've repeated all these methods multiple times, and am certain I'm not mistyping or miscopying the code.  There just seems to be something about the premade files themselves; some configuration setting or something else I'm missing entirely...that will cause them to compile correctly while the files I make myself won't.
80355
I have this struct:  struct Map {     public int Size;      public Map ( int size )     {         this.Size = size;     }      public override string ToString ( )     {         return String.Format ( "Size: {0}", this.Size );     } }   When using array, it works:  Map [ ] arr = new Map [ 4 ] {     new Map(10),     new Map(20),     new Map(30),     new Map(40)};  arr [ 2 ].Size = 0;   But when using List, it doesn't compile:  List&lt;Map&gt; list = new List&lt;Map&gt; ( ) {     new Map(10),     new Map(20),     new Map(30),     new Map(40)};  list [ 2 ].Size = 0;   Why?
1825
Occasionally I come across methods with an uncomfortable number of parameters.  More often than not, they seem to be constructors.  It seems like there ought to be a better way, but I can't see what it is.    return new Shniz(foo, bar, baz, quux, fred, wilma, barney, dino, donkey)   I've thought of using structs to represent the list of parameters, but that just seems to shift the problem from one place to another, and create another type in the process.  ShnizArgs args = new ShnizArgs(foo, bar, baz, quux, fred, wilma, barney, dino, donkey) return new Shniz(args);   So that doesn't seem like an improvement.  So what is the best approach?
113468
I am working with an open-source UNIX tool that is implemented in C++, and I need to change some code to get it to do what I want. I would like to make the smallest possible change in hopes of getting my patch accepted upstream. Solutions that are implementable in standard C++ and do not create more external dependencies are preferred.  Here is my problem. I have a C++ class -- let's call it "A" -- that currently uses fprintf() to print its heavily formatted data structures to a file pointer. In its print function, it also recursively calls the identically defined print functions of several member classes ("B" is an example). There is another class C that has a member std::string "foo" that needs to be set to the print() results of an instance of A. Think of it as a to_str() member function for A.  In pseudocode:  class A { public:   ...    void print(FILE* f);   B b;    ...   };  ...  void A::print(FILE *f) {   std::string s = "stuff";   fprintf(f, "some %s", s);   b.print(f); }  class C {   ...   std::string foo;   bool set_foo(std::str);   ... }  ...  A a = new A(); C c = new C();  ...  // wish i knew how to write A's to_str() c.set_foo(a.to_str());   I should mention that C is fairly stable, but A and B (and the rest of A's dependents) are in a state of flux, so the less code changes necessary the better. The current print(FILE* F) interface also needs to be preserved. I have considered several approaches to implementing A::to_str(), each with advantages and disadvantages:   Change the calls to fprintf() to sprintf()   I wouldn't have to rewrite any format strings print() could be reimplemented as: fprint(f, this.to_str()); But I would need to manually allocate char[]s, merge a lot of c strings , and finally convert the character array to a std::string  Try to catch the results of a.print() in a string stream   I would have to convert all of the format strings to &lt;&lt; output format. There are hundreds of fprintf()s to convert :-{ print() would have to be rewritten because there is no standard way that I know of to create an output stream from a UNIX file handle (though ).  Use Boost's string    More external dependencies. Yuck. Format's syntax is different enough from printf() to be annoying:    printf(format_str, args) -> cout &lt;&lt; boost::format(format_str) % arg1 % arg2 % etc Use Qt's    A different external dependency.    So, have I exhausted all possible options? If so, which do you think is my best bet? If not, what have I overlooked?  Thanks.
102822
I know about , but in my case I need something that implements List, and not Set. So is there an implementation out there, in the API or elsewhere?  It shouldn't be hard to implement myself, but I figured why not ask people here first?
42238
This code won't compile:  \documentclass{article} \usepackage{listings} \lstset{     language=C,     basicstyle=\small }  \begin{document} lorem ipsum\footnote{% \begin{lstlisting} // some C code or whatever... printf("Blah blah\n"); \end{lstlisting}} \end{document}   It says Illegal parameter number in definition of \lst@insertargs. I know listings is really picky about where it goes, but there must be a way to cram a listing into a footnote, right?  (This is a MWE and my tex files are a bit more complex; my actual error is Paragraph ended before \lst@next was complete. but they are hopefully related; I couldn't get this error with the MWE.)
3278
So, I understand that the following doesn't work, but why doesn't it work?  interface Adapter&lt;E&gt; {}  class Adaptulator&lt;I&gt; {     &lt;E, A extends I &amp; Adapter&lt;E&gt;&gt; void add(Class&lt;E&gt; extl, Class&lt;A&gt; intl) {         addAdapterFactory(new AdapterFactory&lt;E, A&gt;(extl, intl));     } }   The add() method gives me a compile error, "Cannot specify any additional bound Adapter&lt;E&gt; when first bound is a type parameter" (in Eclipse), or "Type parameter cannot be followed by other bounds" (in IDEA), take your pick.  Clearly you're just Not Allowed to use the type parameter I there, before the &amp;, and that's that. (And before you ask, it doesn't work if you switch 'em, because there's no guarantee that I isn't a concrete class.) But why not? I've looked through Angelika Langer's FAQ and can't find an answer.  Generally when some generics limitation seems arbitrary, it's because you've created a situation where the type system can't actually enforce correctness. But I don't see what case would break what I'm trying to do here. I'd say maybe it has something to do with method dispatch after type erasure, but there's only one add() method, so it's not like there's any ambiguity...  Can someone demonstrate the problem for me?
93459
I have a dll that contains a templated class. Is there a way to export it without explicit specification?
1296
Why isn't  generic?    Seems like Collection&lt;E&gt; could have boolean remove(E o);   Then, when you accidentally try to remove (for example) Set&lt;String&gt; instead of each individual String from a Collection&lt;String&gt;, it would be a compile time error instead of a debugging problem later.